Easily send mail from a bash script

Submit to reddit

Did you ever need to have e-mail’s sent from within a bash script? Maybe alert you of errors encountered? Me too.

Here’s an easy way on how to do it:

#!/bin/bash
# email send script example
#
# subject of email
SUBJECT="BASH SAYS HELLO"
# destination
EMAIL="user@yourdomain.com"
# Email body
EMAILMESSAGE="/tmp/messagebody.txt"
echo "Email sent from BASH" > $EMAILMESSAGE
echo "Another text line" >> $EMAILMESSAGE
# send message using /bin/mail
/bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE

Easy.

  • Sadana

    nope !!! this script is not working …

    • WordPress mushed the characters, it’s fixed now

  • unknown

    my mail requires Ctrl-D from the command line with this script to send the message.  How does one get around this?