[shell] 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" [email protected]

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

Linux Mailx:

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

This question is related to shell mailx

The answer is


You can try this:

(cat ./body.txt)|mailx -s "subject text" -a "attchement file" [email protected]

The best way is to use mpack!

mpack -s "Subject" -d "./body.txt" "././image.png" mailadress

mpack - subject - body - attachment - mailadress


Johnsyweb's answer didn't work for me, but it works for me with Mutt:

echo "Message body" | mutt -s "Message subject" -a myfile.txt [email protected]

On RHEL Linux, I had trouble getting my message in the body of the email instead of as an attachment . Using od -cx, I found that the body of my email contained several /r. I used a perl script to strip the /r, and the message was correctly inserted into the body of the email.

mailx -s "subject text" [email protected] < 'body.txt'

The text file body.txt contained the char \r, so I used perl to strip \r.

cat body.txt | perl success.pl > body2.txt
mailx -s "subject text" [email protected] < 'body2.txt'

This is success.pl

    while (<STDIN>) {
        my $currLine = $_;
        s?\r??g;
        print
    }
;

Try this it works for me:

(echo "Hello XYX" ; uuencode /export/home/TOTAL_SI_COUNT_10042016.csv TOTAL_SI_COUNT_10042016.csv ) | mailx -s 'Script test' [email protected]