mailx is a Unix utility program that is used for sending and receiving mail and is an improved version of the mail utility. This article will focus on only sending emails using the mailx utility within a shell script.
Syntax: mailx [-s subject] [-a attachment] [-r from addr] to-addr
You can put the body of the subject right after the mailx command and terminate it with EOT (End of Transmission). However I find it easier to simply write your entire output to a temporary file and email the file content as the body of the email. This way the mailx command is only 1 line.
Usage:
#!/bin/bash
subject="Email Subject"
to="to@address.com"
from="from@address.com"
echo This is the body of the email >> sendmail.tmp
echo This is the 2nd line in the email >> sendmail.tmp
echo `date` : is the date >> sendmail.tmp
mailx -s{$subject} -r "YourName<$from>" $to < sendmail.tmp