Easily send mail from a bash script

March 18, 2010
By

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/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.

Tags: , , , , ,

  • Sadana

    nope !!! this script is not working …

  • http://www.sgvulcan.com icebox

    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?

Follow Me

Subscribe via RSS