将 bash 脚本中的 sendmail 用于多个收件人
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13390894/
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
Using sendmail from bash script for multiple recipients
提问by Amir
I'm running a bash script in cron to send mail to multiple recipients when a certain condition is met.
我正在 cron 中运行 bash 脚本,以便在满足特定条件时将邮件发送给多个收件人。
I've coded the variables like this:
我已经编码了这样的变量:
subject="Subject"
from="[email protected]"
recipients="[email protected] [email protected]"
mail="subject:$subject\nfrom:$from\nExample Message"
And the actual sending:
和实际发送:
echo -e $mail | /usr/sbin/sendmail "$recipients"
The problem is that only [email protected] is receiving the email. How can I change this so all the recipients receive the email?
问题是只有 [email protected] 正在接收电子邮件。如何更改此设置以便所有收件人都能收到电子邮件?
NOTE: The solution has to be with sendmail, I'm using jailshell and it seems to be the only available method
注意:解决方案必须使用 sendmail,我使用的是 jailshell,它似乎是唯一可用的方法
回答by Gilles Quenot
Try doing this :
尝试这样做:
recipients="[email protected],[email protected],[email protected]"
And another approach, using shell here-doc:
另一种方法,使用 shell here-doc:
/usr/sbin/sendmail "$recipients" <<EOF
subject:$subject
from:$from
Example Message
EOF
Be sure to separate the headers from the body with a blank line as per RFC 822.
请务必按照RFC 822使用空行将标头与正文分开。
回答by Harijs Krūtainis
Use option -t for sendmail.
对 sendmail 使用选项 -t。
in your case - echo -e $mail | /usr/sbin/sendmail -t
and add yout Recepient list to message itself like To: [email protected] [email protected]
right after the line From:.....
在您的情况下 -echo -e $mail | /usr/sbin/sendmail -t
并将您的收件人列表添加到消息本身,就像To: [email protected] [email protected]
在该行之后一样From:.....
-t
option means -
Read message for recipients. To:, Cc:, and Bcc: lines will be scanned for recipient addresses. The Bcc: line will be deleted before transmission.
-t
选项意味着 - 为收件人阅读消息。To:、Cc: 和 Bcc: 行将扫描收件人地址。Bcc: 行将在传输前被删除。
回答by Denish Thummar
to use sendmail from the shell script
从 shell 脚本使用 sendmail
subject="mail subject"
body="Hello World"
from="[email protected]"
to="[email protected],[email protected]"
echo -e "Subject:${subject}\n${body}" | sendmail -f "${from}" -t "${to}"