Skip to content

Instantly share code, notes, and snippets.

@Mistat
Created February 5, 2012 13:49
Show Gist options
  • Save Mistat/1745673 to your computer and use it in GitHub Desktop.
Save Mistat/1745673 to your computer and use it in GitHub Desktop.
Mail Attachment sample
#!/bin/bash
# vim: enc=utf8:
CMD_SENDMAIL=/usr/sbin/sendmail
CMD_UUENC=/usr/bin/uuencode
CMD_NKF=/usr/local/bin/nkf
ATTACHMENT_FILE=test.jpg
BOUNDARY=`date +%Y%m%d%H%M%N`
CONTENT_TYPE=`file --mime $ATTACHMENT_FILE | cut -d' ' -f2`
ATTACHMENT_FILENAME=`basename $ATTACHMENT_FILE`
mimeencode() {
echo $@ | nkf -Wj -M
}
MAIL_FROM="$(mimeencode 'テスト') <[email protected]>"
MAIL_TO="$(mimeencode 'テスト') <[email protected]>"
SUBJECT="$(mimeencode 'ファイル添付テスト')"
$CMD_NKF -Wj -m0 <<HERE | "$CMD_SENDMAIL" "$MAIL_ADDRESS";
Subject: ${SUBJECT}
From: ${MAIL_FROM}
To: ${MAIL_TO}
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="${BOUNDARY}"
Content-Transfer-Encoding: 7bit
--${BOUNDARY}
Content-Type: text/plain; charset=ISO-2022-JP
ファイルを添付します。
--${BOUNDARY}
Content-Transfer-Encoding: base64
Content-Disposition : attachment; filename="$(mimeencode $ATTACHMENT_FILE)"
$($CMD_UUENC -m $ATTACHMENT_FILE $ATTACHMENT_FILENAME | sed -e '$d' | sed -e '1d')
--${BOUNDARY}--
HERE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment