问题
I'm running a bash script in cron to send mail to multiple recipients when a certain condition is met.
I've coded the variables like this:
subject="Subject"
from="user@domain.com"
recipients="user1@gmail.com user2@gmail.com"
mail="subject:$subject\nfrom:$from\nExample Message"
And the actual sending:
echo -e $mail | /usr/sbin/sendmail "$recipients"
The problem is that only user2@gmail.com is receiving the email. How can I change this so all the recipients receive the email?
NOTE: The solution has to be with sendmail, I'm using jailshell and it seems to be the only available method
回答1:
Try doing this :
recipients="user1@gmail.com,user2@gmail.com,user3@gmail.com"
And another approach, using 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.
回答2:
Use option -t for sendmail.
in your case - echo -e $mail | /usr/sbin/sendmail -t
and add yout Recepient list to message itself like To: someone@somewhere.com someother@nowhere.com
right after the line 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.
回答3:
to use sendmail from the shell script
subject="mail subject"
body="Hello World"
from="me@domain.com"
to="recipient1@domain.com,recipient2@domain.com"
echo -e "Subject:${subject}\n${body}" | sendmail -f "${from}" -t "${to}"
来源:https://stackoverflow.com/questions/13390894/using-sendmail-from-bash-script-for-multiple-recipients