使用命令行和sendmail发送包含多个附件的电子邮件

时间:2014-01-20 10:11:25

标签: bash email sendmail uuencode

是否可以使用uuencodesendmail发送多个附件?

在脚本中,我有一个变量,其中包含需要附加到单个电子邮件的文件,如:

$attachments=attachment_1.pdf attachment_2.pdf attachment_3.pdf attachment_4.pdf

还有一个$template变量,如:

$template="Subject: This is the subject
From: no-reply@domain.com
To: %s
Content-Type: text/plain

This is the body.
"

我想出了:

printf "$template" "$recipient" |
sendmail -oi -t

在此我必须在$attachments变量中附加所有内容?

2 个答案:

答案 0 :(得分:4)

attachments="attachment_1.pdf attachment_2.pdf attachment_3.pdf attachment_4.pdf"
recipient='john.doe@example.net'

# () sub sub-shell should generate email headers and body for sendmail to send
(
# generate email headers and begin of the body asspecified by HERE document 
cat - <<END
Subject: This is the subject
From: no-reply@domain.com
To: $recipient
Content-Type: text/plain

This is the body.

END
# generate/append uuencoded attachments
for attachment in $attachments ; do
  uuencode $attachment $attachment
done
) | /usr/sbin/sendmail -i -- $recipient

答案 1 :(得分:0)

就其价值而言,mailx也很好用。

mailx -s "Subject" -a attachment1 -a attachement2 -a attachment3 email.address@domain.com < /dev/null