So I know how to use the mail command to send mail through ssh, as in typing mail -s "Subject here" user@host.com, then typing out the message, hitting CTRL + D to stop composing, then press enter to not have a Cc.

How I'm curious, how would I send an email through a non-interactive shell script? Just so I can send an email if a backup decided to fail, or whatever reason.

Thanks a bunch!

link|improve this question

feedback

2 Answers

up vote 6 down vote accepted

Well, just pipe in the content. eg

echo "message text"|mail -s "subject" someone@example.com
link|improve this answer
you could fake the FROM: field in the same way JOTN adds arguments for easier filtering (eg monitorin@myserver) – mbx Mar 12 '11 at 0:20
feedback

I do it like this:

/usr/lib/sendmail -t <<EOF 
To: someone@example.com
Subject: My script works

Looks like my script is working.
EOF 

You can use shell variables in the message. You can also add CC and BCC lines as desired.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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