メール:コマンドラインから添付ファイルを含む電子メールを送信する

メール:コマンドラインから添付ファイルを含む電子メールを送信する

コマンドライン(スクリプト)から電子メールを送信する方法を知っています。

echo "body" | mail -s "subject" [email protected]

コマンドライン(スクリプト)でも添付ファイルを送信できますか?

heirloom-mailx私はDebianでWheezyを使用しています。

ベストアンサー1

簡単な方法:使用uuencodesharutilsパッケージの一部)。書式やテキストは使用できません。添付ファイルとカスタムタイトルを含む電子メールだけが必要です。

uuencode /path/to/file file_name.ext | mail -s subject [email protected]

複雑な方法:sendmailHTML形式を使用する:

v_mailpart="$(uuidgen)/$(hostname)"
echo "To: [email protected]
Subject: subject
Content-Type: multipart/mixed; boundary=\"$v_mailpart\"
MIME-Version: 1.0

This is a multi-part message in MIME format.
--$v_mailpart
Content-Type: text/html
Content-Disposition: inline

<html><body>Message text itself.</body></html>

--$v_mailpart
Content-Transfer-Encoding: base64
Content-Type: application/octet-stream; name=file_name.ext
Content-Disposition: attachment; filename=file_name.ext

`base64 /path/to/file`
 --$v_mailpart--" | /usr/sbin/sendmail -t

添付ファイルが複数ある場合、最後の部分が繰り返されることがあります。

おすすめ記事