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!


Well, just pipe in the content. eg

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

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

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.