46

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

Amir
  • 1,796
  • 4
  • 18
  • 28

4 Answers4

90

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.

T.Rob
  • 31,124
  • 9
  • 58
  • 101
Gilles Quenot
  • 154,891
  • 35
  • 213
  • 206
9

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.

Harijs Krūtainis
  • 1,167
  • 13
  • 12
  • When I pass the `-t` option, I get `sendmail: recipients with -t option not supported`. Any ideas? Thanks. – Vassilis Oct 28 '16 at 09:01
8

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}"
Denish Thummar
  • 199
  • 2
  • 4
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

craigdfrench
  • 902
  • 9
  • 19