Amir
Amir

Reputation: 2258

Using sendmail from bash script for multiple recipients

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

Upvotes: 55

Views: 242311

Answers (4)

Denish Thummar
Denish Thummar

Reputation: 219

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

Upvotes: 10

Harijs Krūtainis
Harijs Krūtainis

Reputation: 1270

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.

Upvotes: 10

Gilles Quénot
Gilles Quénot

Reputation: 184955

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.

Upvotes: 97

Chris Sat
Chris Sat

Reputation: 1

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

Upvotes: 0

Related Questions