Subscribe to RSS

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.

Please share the love of this post by bookmarking it, and sharing it with others. Thanks!

  • Digg
  • del.icio.us
  • description
  • Reddit
  • Technorati
  • BlinkList
  • E-mail this story to a friend!
  • Facebook
  • Live
  • MisterWong
  • Netvouz
  • NewsVine
  • Slashdot
  • SphereIt

5 Comments

  1. micharg
    Posted 11 June, 2007 at 09:24 | Permalink

    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
    Posted 8 July, 2007 at 23:50 | Permalink

    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
    Posted 9 July, 2007 at 16:55 | Permalink

    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
    Posted 26 May, 2008 at 22:31 | Permalink

    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. Posted 26 May, 2008 at 23:47 | Permalink

    @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.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*