55

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="[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?

NOTE: The solution has to be with sendmail, I'm using jailshell and it seems to be the only available method

4 Answers 4

97

Try doing this:

recipients="[email protected],[email protected],[email protected]"

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.

Sign up to request clarification or add additional context in comments.

Comments

10

Use option -t for sendmail.

in your case - echo -e $mail | /usr/sbin/sendmail -t and add your recipient list to message itself like To: [email protected] [email protected] 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.

2 Comments

When I pass the -t option, I get sendmail: recipients with -t option not supported. Any ideas? Thanks.
-t is running fine with smaller busybox sendmail version, cf. busybox.net/downloads/BusyBox.html#sendmail
10

to use sendmail from the shell script

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}"

Comments

0

For postfix sendmail, I am adding one line command useful for scripting

I had problem adding recipients in default position in the end of sendmail command in RHEL (Undisclosed Recipients) and piping echo command saved the day.

Option -f found from http://www.postfix.org/sendmail.1.html

Please note that syntax in echo is important, try echo to a file to check before attempting with sendmail.

echo -e "To:receiver1@domain1, receiver2@domain2 \nSubject:Subject of email \n\nBody of email.\n" | /usr/sbin/sendmail -f sender@domain -F sendername -it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.