bash SMTP

There might arise situations, where a monitoring script is in dire need to send an e-mail. But how do you send mail when for some reason the mail system itself is dysfunctional?  Bash to the rescue.

Building upon my previous post on bash networking, I put together a bash_mail function, which is capable of connecting directly to an SMTP server (without authentication or SSL) and send email message. The function respects best practices and tries not to trigger anti-spam mechanisms by dutifully awaiting SUCCESS response codes from server and aborting on server fail code. The function returns 0 when all went OK and 1 or 2 depending on the state it dropped off.

UPDATE: Taken into account ideas from a coworker, I tweaked the code to (1) run in subshell to release filedescriptor requirements, (2) use loop for DRY and (3) drop cat usage to be as much as possible self-sustained.

UPDATE 2: Script adjusted to comply to SMTP standards regarding <CRLF> instead of simple <LF> as pointed out by Zoot in the comments.

The function can be used as a function in other scripts. Just source it in, where needed. You can also define the arguments, without rewriting the function:

source bash_mail.sh
# just send the mail with hard-coded parameters
bash_mail
 
# define important parameters
bash_mail "me@example2.com2" "Still Panic"

Related posts:

  1. Bash and Networking
  1. WOW!..this code is ingenous, beautiful, simple and elegant….I love it! :-)

  2. Thank you!

    If you find usage for this, I would like to hear how and where.

  3. Starting with the “where?”….Amsterdam, Netherlands ! :-)

    At the moment I’m finetuning a fully automated video conversion server whitch is completely event driven and parallell processing.
    I’ve written this in bash on top of a regular server distro. Why bash ?

    Well, because of the challenge; maybe I wanted to prove that it could be done, and at least learn something from it :-)
    Also I always think that good code needs to be modular, elegant, and posess a kind of beauty by simplicity; now i had an opportunity to proof that also, with a complex project like this :-)

    This server is running rock-steady for almost a year now.
    I toyed for a while with the idea of mailing important events to the “sysop” (like logging by email) but didn’t want a full-blown pre-installed SMTP monster.

    Then a friend of mine found your script. wow! This is exactly what is needed for the job and above all: featherlight, elegant, lucid and in bash; couldn’t be better :-)

    Soon I’ll have the command-by-ftp on a more mature level together with a sort of report agent or message unit, that is the moment I would like to incorporate the SMTP-client.

    Also have to clean out some rats nests from the process-controller-code, it is readable but has no beauty.
    Well, I have a week off and it’s raining here so I suspect I will make some progress ;-)

  4. Elegant script, thanks!

    However, it sends Bare LFs, which thousands of mail servers will reject:
    http://smtpd.develooper.com/barelf.html

    Here’s a diff file I generated, so you can patch your script to be compliant :)

    http://pastie.org/1017368

  5. Thank you!
    I updated the script accordingly.

    Also I uploaded it to Gist to enable easier download and modifications.

  6. cool! one thing, variables within a subshell is local to the subshell. so, bash_mail function would return a 1 (since $i doesn’t exist for the parent the function returns the default of 1).

  7. You are correct of course. I added the subshell in a later phase, but never tested the fail-handling again.

    I updated the code accordingly.

  8. Send mail using Bash | In da Wok ...... - pingback on 30. dets 2010 at 07:44

Leave a Comment

* Copy this password:

* Type or paste password here:


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Trackbacks and Pingbacks: