shellmailx

Sending simple message body + file attachment using Linux Mailx


I am writing a shell script to send an email using Linux Mailx, the email must contain a file attachment and a message body.

Currently sending an email with an attachment:

output.txt | mail -s "Daily Monitoring" james@dell.com

I wish to add a message body. How should i?

Linux Mailx:

mail [-eIinv] [-a header] [-b addr] [-c addr] [-s subj] to-addr

Solution

  • The usual way is to use uuencode for the attachments and echo for the body:

    (uuencode output.txt output.txt; echo "Body of text") | mailx -s 'Subject' user@domain.com
    

    For Solaris and AIX, you may need to put the echo statement first:

    (echo "Body of text"; uuencode output.txt output.txt) | mailx -s 'Subject' user@domain.com