I'm using alexkowalski script from Email using shell script

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

My question is how to read MAILADDR array from an external flat file?

And how to insert an external flat file replacing the body of the email as in

echo"/home/user/information-for-any-user.txt"  | mail -s "Mail test subject..." $i ??

If the "external flat file" from which the email addresses are taken from doesn't look too funny (is well formatted), you could use something like that :

mail -s "My subject" $(cat /this/is/the/external/flat/file.txt) < /home/user/information-for-any-user.txt

You can provide multiple address on the mail command.

  1. assume there are no glob chars in the flat file, and the email addresses are separated by any whitespace:

    echo "Mail test..." | mail -s "Mail test subject..." $(< address.file)
  2. read the newline-separated list of addresses into a bash array

    mapfile -t addr < address.file
    echo "Mail test..." | mail -s "Mail test subject..." "${addr[@]}"

Many thanks, combining both answers did the job.

mapfile -t addr < /home/user/address-file #(one email address per line)

echo "$(cat < /home/user/information-for-any-user)" | mail -s "My subject" "${addr[@]}"


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.