Linuxのコマンドラインで添付ファイル付きのメールを送ります。
添付ファイルを「archive」以下に配置 本文を「mail/body.txt]に配置しています。
vim executemail.sh -------------------------------------- #!/bin/sh TO=$1 ATTACH_NAME=$2 if [ "$TO" = "" ]; then echo "usage: $0 mailaddress attach_file_name" exit 1 fi SUBJECT="メール添付テスト" MAIL_BOUNDARY=`date +%Y%m%d%H%M%N` ATTACH_FILE_PATH=archive/${ATTACH_NAME} ATTACH_FILE_NAME=${ATTACH_NAME} sendmail -t << EOF From: ${FROM} To: ${TO} Subject: ${SUBJECT} MIME-Version: 1.0 Content-type: multipart/mixed; boundary=${MAIL_BOUNDARY} Content-Transfer-Encoding: 7bit --${MAIL_BOUNDARY} Content-type: text/plain; charset=iso-2022-jp Content-Transfer-Encoding: 7bit `cat mail/body.txt` --${MAIL_BOUNDARY} Content-type: application/zip; name=${ATTACH_FILE_NAME} Content-Transfer-Encoding: base64 Content-Disposition : attachment; filename=${ATTACH_FILE_NAME} `cat ${ATTACH_FILE_PATH} | base64` --${MAIL_BOUNDARY}-- EOF -------------------------------------- chmod +x executemail.sh ./executemail.sh sample@test.com attach.zip
というように使います。
複数のファイルを添付する場合は
--${MAIL_BOUNDARY} Content-type: application/zip; name=${ATTACH_FILE_NAME} Content-Transfer-Encoding: base64 Content-Disposition : attachment; filename=${ATTACH_FILE_NAME} `cat ${ATTACH_FILE_PATH} | base64` --${MAIL_BOUNDARY} Content-type: application/zip; name=${ATTACH_ALT_FILE_NAME} Content-Transfer-Encoding: base64 Content-Disposition : attachment; filename=${ATTACH_BATCH_FILE_NAME} `cat ${ATTACH_ALT_FILE_PATH} | base64` --${MAIL_BOUNDARY}--
というようになります。
参考:http://qiita.com/f81@github/items/6ef614c6e88b9d17c41f