bash 使用 uuencode 将变量中的多个附件附加到电子邮件并使用 sendmail 发送

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19940292/
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 08:36:48  来源:igfitidea点击:

Using uuencode to attach multiple attachments from a variable to an e-mail and send using sendmail

bashsendmailuuencode

提问by user2656114

I have a script that currently does:

我有一个当前执行的脚本:

cat $body | uuencode $attachment $attachment | sendmail $sender -t

What should I adjust so that $attachment could be multiple attachments? I have come up with the below but it doesn't look correct?

我应该调整什么才能使 $attachment 可以是多个附件?我想出了以下内容,但看起来不正确?

cat $body |
for i in $attachments 
do
uuencode $i $i
done
| sendmail $sender -t

回答by iruvar

Typically, you don't want to store a list of file names in a parameter. With default IFS, spaces embedded within file names will give rise to problems. Instead, declare an array with files

通常,您不想在参数中存储文件名列表。默认情况下IFS,文件名中嵌入的空格会引起问题。相反,声明一个包含文件的数组

a=(file1 file2 file3 file4)
(for file in "${a[@]}"; do uuencode "$file" "$(basename "$file")"; done) |
 sendmail $sender -t

回答by AnFi

Try the following script:

尝试以下脚本:

# specify list of email recipients
recipients=...
# specify envelope sender address
sender=...
( 
  cat $body
  for i in $attachments 
  do
    uuencode $i $i
  done
) | sendmail -f$sender -i -- $recipients
  • $bodyfile must contain message headers (e.g. Subject:) separated by an empty line from message body
  • IMHO it is a better/safer style to specify recipients via command line instead of making sendmail extract them from headers.
  • $body文件必须包含消息头(例如Subject:),由消息正文中的空行分隔
  • 恕我直言,通过命令行指定收件人而不是让 sendmail 从标题中提取它们是一种更好/更安全的风格。

回答by Dan

FILES="/rollovers/DailyCadRpt.* /rollovers/DailyFireRpt.*"

文件=“/rollovers/DailyCadRpt.* /rollovers/DailyFireRpt.*”

(for f in $FILES ; do uuencode "$f" "$f" ; done ) | mail -s "Subject" [email protected]

(对于 $FILES 中的 f ; uuencode "$f" "$f" ; done ) | mail -s "主题"[email protected]

The above works in AIX 6.1 for wildcards. But, you must use the 10-pad asterisk. The asterisk above the number eight does not work in AIX. Also, this does not have any body text. But that is done as in the other examples. You may add more files by using a space as the separator, as in my example. Also, you cannot use Daily* with either asterisk. AIX just won't do it. The asterisk must come after a period in the file name. Our reports have the date added to the report name separated by a period. It preserves our archival naming pattern and grabs it every day without needing a specific file name.

以上在 AIX 6.1 中适用于通配符。但是,您必须使用 10-pad 星号。数字 8 上方的星号在 AIX 中不起作用。此外,这没有任何正文。但这是在其他示例中完成的。您可以使用空格作为分隔符来添加更多文件,如我的示例所示。此外,您不能将 Daily* 与任一星号一起使用。AIX 只是不会这样做。星号必须在文件名中的句点之后。我们的报告将日期添加到报告名称中,以句点分隔。它保留了我们的档案命名模式并每天抓取它而无需特定的文件名。