How to use mailx to send file as text and add extra text to email body?

homer picture homer · Jan 20, 2015 · Viewed 21k times · Source

How to send some text in email along with the contents of the file, don't want to send file as an attachment? is it possible via mailx command?

mailx -s "Email log file" [email protected] <$log_file;

$log_file contents gets emailed but below doesn't work

echo "Comment: log contains last month report" | mailx -s "Email log file" [email protected] < $log_file

Needed output in email:

Comment: Log contains last month report

 <All contents of $LOG_FILE as text>

Answer

JeffM picture JeffM · Apr 19, 2016

Here is how you would do it:

echo "Comment: log contains last month report\n $(cat \$log_file)" | mailx -s "Email log file" [email protected]

The only thing "funny" is you'll have to escape that $ in the filename. You can also add more new lines \n if need be.