2

I'm using alexkowalski script from Email using shell script

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

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 ??
3

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
3

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[@]}"
    
0

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[@]}"

Cheers

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.