Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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!

share|improve this question

2 Answers 2

up vote 8 down vote accepted

Well, just pipe in the content. eg

echo "message text"|mail -s "subject" someone@example.com
share|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

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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