It must be soooo simple, but all of the examples that I found after a few hours of googling don't work, as the mail is always attached to the file instead of writing it into the body. I don't want to send empty messages either, but want to send mail only when I have output from the script. Here is my command:

$script_file  > /tmp/output.log ; mail -E -s "Output subject" "my@emailaddress"  < /tmp/output.log ; rm -f /tmp/output.log

Would it be possible to change this command to force the mail to embed the content of the output.log file?

My system is CentOS.

Many thanks!

  • 1
    Skip the whole output file completely and do $script_file | mail -E -s "Output subject" "my@emailaddress" – HBruijn Jun 19 '15 at 12:06
  • Thanks, but your example is just send an email and doesn't embed my file into the body. – t.fazakas Jun 19 '15 at 12:09

I use mutt to do that, like this. Be aware of argument order, do not place recipient after -a

mutt joe@example.com -s "Mail subject" -a /file/to/attach < /file/with/mail/body

  • Thanks Dan, it's almost perfect as the content of the file is already in the email body! Is it possible to have the same result but without attaching the same file? – t.fazakas Jun 19 '15 at 12:02
  • This is working: mutt -s "Mail subject" joe@example.com < /file/with/mail/body but I still got the empty email messages when the output file is blank. – t.fazakas Jun 19 '15 at 12:18
  • Wrap it around an empty file test, like if -s /mail/body; then mutt bla bla; fi – Dan Jun 19 '15 at 13:00

You can use cat and pipe it to redirect the output to mail.

$script_file > /tmp/output.log ; cat /tmp/output.log | mail -s "Output subject" "my@emailaddress" ; rm -f /tmp/output.log

(I'm not sure there is -E though so I removed it)

  • Thanks, I've found this solution though and just tried again but it still attached the file :( – t.fazakas Jun 19 '15 at 11:55
  • As mutt is working for you it seems to be MIME issue. You need to define mime type say text/plain add set content_type=text/plain mutt -e set content_type=text/plain joe@example.com -s "Mail subject" -a /file/to/attach < /file/with/mail/body while sending email out it always checks the MIME type and it seems its not able to figure out what MIME type to use by default for the script output file and its sending empty attachments. – chetangb Jun 19 '15 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.