2

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!

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

2 Answers 2

2

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

mutt [email protected] -s "Mail subject" -a /file/to/attach < /file/with/mail/body

3
  • 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? Commented Jun 19, 2015 at 12:02
  • This is working: mutt -s "Mail subject" [email protected] < /file/with/mail/body but I still got the empty email messages when the output file is blank. Commented Jun 19, 2015 at 12:18
  • Wrap it around an empty file test, like if -s /mail/body; then mutt bla bla; fi Commented Jun 19, 2015 at 13:00
0

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)

2
  • Thanks, I've found this solution though and just tried again but it still attached the file :( Commented Jun 19, 2015 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 [email protected] -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. Commented Jun 19, 2015 at 14:09

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.