bash 使用命令行和 sendmail 发送带有多个附件的电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21231174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:17:57  来源:igfitidea点击:

Send e-mail with multiple attachments using command line and sendmail

bashemailsendmailuuencode

提问by user2656114

Is it possible to send multiple attachments with uuencodeand sendmail?

是否可以使用uuencode和发送多个附件sendmail

In a script I have a variable containing the files that need to be attached to a single e-mail like:

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

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

Also a $templatevariable like:

还有一个$template变量,如:

$template="Subject: This is the subject
From: [email protected]
To: %s
Content-Type: text/plain

This is the body.
"

I have come up with:

我想出了:

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

Somewhere within this I must attach everything in the $attachmentsvariable?

在此范围内的某个地方,我必须附加$attachments变量中的所有内容?

回答by AnFi

attachments="attachment_1.pdf attachment_2.pdf attachment_3.pdf attachment_4.pdf"
recipient='[email protected]'

# () 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: [email protected]
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