Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I know how to use the mail command to send mail through ssh, as in typing mail -s "Subject here", 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
up vote 8 down vote accepted

Well, just pipe in the content. eg

echo "message text"|mail -s "subject"
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 
Subject: My script works

Looks like my script is working.

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

share|improve this answer

Your Answer


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.