bashshellkshmailx

mailx sending email to multiple accounts


I am trying to use mailx to send an email via shell script.

Message=<HTML><BODY><p>FINISHED</p></BODY></HTML>

Recipients=email1@email.org;email2@email.org
Recipients=$(echo "${Recipients}" | sed "s/;/ /g")
echo "Recipients:  ${Recipients}"

mailx -s "Ingestion Report ${EXT1}. $( echo "\nContent-Type: text/html")" "${Recipients}" < $MESSAGE

my problem is I'm trying to change the list delimited by a semi-colon to a space-delimited list because I"m told that is what's needed by mailx.

However, the response is:

sh: email2@email.org:  not found

what am I doing wrong? thanks.


Solution

  • A semicolon is a command separator, so you have to change how you are defining Recipients from:

    Recipients=email1@email.org;email2@email.org
    

    To:

    Recipients="email1@email.org;email2@email.org"
    

    Quoting the value prevents the ; from being interpreted as a command separator.

    Alternatively, you can just define Recipients correctly in the first place:

    Recipients="email1@email.org email2@email.org"
    

    Or if you don't control that for whatever reason, you can drop the sed call and just do:

    mailx ... ${Recipients/;/ }