Send mail with a BASH Shell Script

Like any good programmer, I try to automate the crap out of everything. If you have to do it more than once, I try to write a script for it.

This time I want to show you how you can easily send an e-mail from a BASH script. The idea is that you want the script to send out an email to notify a user that something has happened.

We’re going to use the GNU Mail utility here. The basic syntax to send an email is like this:

/usr/bin/mail -s "Subject" someone@example.org < message.txt

The trick when using this in a shell script is creating and using the message.txt file correctly.

Let’s setup the basis first:

#!/bin/bash
SUBJECT="Automated Security Alert"
TO="alarms@ariejan.net"
MESSAGE="/tmp/message.txt"
 
/usr/bin/mail -s "$SUBJECT" "$TO" < $MESSAGE

All we need to do now is create the message. In this example we’re going to notify the receiver that something happened at a certain time. We can use the append (>>) operator to add text to the message file. Afterwards, we must remove the temporary message file, of course. The complete script now becomes:

#!/bin/bash
SUBJECT="Automated Security Alert"
TO="alarms@ariejan.net"
MESSAGE="/tmp/message.txt"
 
echo "Security breached!" >> $MESSAGE
echo "Time: `date`" >> $MESSAGE
 
/usr/bin/mail -s "$SUBJECT" "$TO" < $MESSAGE
 
rm $MESSAGE

The email will contain the a timestamp from when the mail was sent.

This method is great for letting an administrator now if something happened. Maybe you need to check if your webserver is up and running. This script can an administrator about the issue.

  • Twitter
  • Digg
  • del.icio.us
  • DZone
  • Reddit
  • email

9 Responses to “Send mail with a BASH Shell Script”

  1. micharg says:

    Script which does the same job (on the fly) without creating a temporary file:

    #!/bin/bash
    SUBJECT="Automated Security Alert"
    TO=”alarms@ariejan.net”
     
    /usr/bin/mail -s "$SUBJECT" "$TO" <<EOF
    Security breached!
    Time: `date`
    EOF
  2. asker says:

    Do anybody know what i am doing wrong?

    script:

    #! /bin/sh
    SUBJECT=”SET-EMAIL-SUBJECT”
    EMAIL=”mymail@ad.dy”
    EMAILMESSAGE=”pathtofile”

    echo “This is an email message test” > $EMAILMESSAGE
    echo “This is email text” >> $EMAILMESSAGE

    #/bin/mail -s “$SUBJECT” “$EMAIL”

  3. ted loud says:

    I want to send out generated mails (in text and html) with attached binaries. the mails are ready, but my thunderbird supports no sending of fully generated source, if i compose the drafts file or import eml. On sending it wants to reattach the binaries and that goes wrong. Therefore i want to send the prepared mail source to my smtp server. Any idea how I could handle this?

  4. syed nusrath mateen says:

    Hi ,
    I tried out what is said but mail is getting bounced back.
    i am pasting you the failure delivery content.

    From: Mail Delivery System
    To: @prodigy
    Subject: Mail delivery failed: returning message to sender
    Date: Tue, 27 May 2008 02:58:50 +0530

    This message was created automatically by mail delivery software.

    A message that you sent could not be delivered to one or more of its
    recipients. This is a permanent error. The following address(es) failed:

    @gmail.com
    Mailing to remote domains not supported

    —— This is a copy of the message, including all the headers. ——

    Return-path: <@prodigy>
    Received: from by prodigy with local (Exim 4.69)
    (envelope-from <@prodigy>)
    id 1K0kFN-000407-QX
    for @gmail.com; Tue, 27 May 2008 02:58:49 +0530

  5. @syed:

    It appears that your MTA (Exim in your case) is not configured to relay mail to remote domains. This is not an issue with the script I posted, but with Exim.

    Check out exim documentation on how to configure it for outgoing email.

  6. proxyd says:

    How to send email with an attachment regular means?

  7. Samuel says:

    Hello,

    How can we edit the ‘From’ field in mail command ?

  8. EasyDaMan says:

    You could also use a function to show your friends how cool you are when it comes to bash-scripting.

    function email {
    /usr/bin/mail -s “$SUBJECT” “$TO” <<EOF
    $1
    Time: `date`
    EOF
    email "Hello, your mother is your sister!"
    }

    Now, $1 is the first parameter that you pass into the function. You could generate some statuses beforehand and then mail them to you or whatever!

    Peace

  9. EasyDaMan says:

    OH… well! The “}” comes after the EOF, of course. Then, email is called after the paren!
    I love you

Leave a Reply