5

Is there a possibility to add multiple e-mails to a single variable so that when the shell script is run, it sends a mail to all the specified emails?

5

Assuming you use sendmail or mail in your script (which both expect a comma separated string as list of recipients), you can concatenate the IDs (or directly write them as such a list):

$: recipients="rec1@example.com, rec2@example.com, rec3@example.com"

or concatenating:

$: base_recipients="important1@example.com, important2@example.com"
$: full_recipients="$base_recipients, special@example.com"

$: echo $full_recipients
important1@example.com, important2@example.com, special@example.com  

The following would be an example sending a mail with sendmail to 3 different mail IDs:

#!/bin/bash

recipients="reci1@example.com, another@example.com, friend3@different.com"
subject="Mail to you all"
from="your_mail@something.com"

message_txt="Hi all!\n This is a message to all 3 of you!.\n cheers, Me."

/usr/sbin/sendmail "$recipients" << EOF
subject:$subject
from:$from
$message_txt
EOF
2

Here's my code using mail array:

MAILADDR=(mail1@example.com mail2@example.com mail3@example.com)
for i in "${MAILADDR[@]}"
    do
         echo "Mail test..." | mail -s "Mail test subject..." $i
    done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.