Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sendmail from bash script for multiple recipients

Tags:

bash

sendmail

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

like image 442
Amir Avatar asked Nov 15 '12 02:11

Amir


People also ask

How do I email a shell script output?

Run `mail' command by '-s' option with email subject and the recipient email address like the following command. It will ask for Cc: address. If you don't want to use Cc: field then keep it blank and press enter. Type the message body and press Ctrl+D to send the email.

How do I send to multiple email addresses in SMTP?

If you want to use smtplib to send email to multiple recipients, use email. Message. add_header('To', eachRecipientAsString) to add them, and then when you invoke the sendmail method, use email.

Can we send email from shell script?

Another way to send email messages on a Linux system is by using the “sendmail” command. This is one of the most common email-sending commands used by Linux system operators. However, you will have to install the “sendmail” program to be able to run this command.


2 Answers

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.

like image 63
Gilles Quenot Avatar answered Oct 07 '22 20:10

Gilles Quenot


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: [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.

like image 31
Harijs Krūtainis Avatar answered Oct 07 '22 18:10

Harijs Krūtainis