Search code examples
bashshmailx

Send email with body and attachment with mailx


How do you send an email with both a body and attachment with mailx?

This code will send an email with a body:

cat $body | tr -d \\r | mailx -s "SUBJECT" [email protected]

tr -d \\r is necessary in this case because if the body is not piped through tr, it will send as a .bin attachment (for this particular body). Found the solution to the body sending as .bin here: Use crontab job send mail, The email text turns to an attached file which named ATT00001.bin

I have tried putting -f $attachment after the subject, but am given the error, More than one file given with -f, and the command will not run.


Solution

  • From mailx's man page :

    -a file
              Attach the given file to the message.
    

    -f makes mailx process a file as if it was provided on stdin, so you encountered an error because you were providing data to mailx both through stdin and a file.