/dev/nikc/blog

Kuolleiden purjehduskenkien seura

Sep 14th 2010

Dummy sendmail for development environments

21:21

Most developers working with the web will sooner or later need to deal with sending e-mail. Usually it’s a contact or feedback form that needs to be delivered to an inbox in a nearby universe.

What has been bugging me about this is that whenever I need to send e-mail, I actually need to use e-mail, which slows things down and is cumbersome in so many ways, since your sysadmin didn’t configure the mailer on the development server – and more often than not, that development environment is your own laptop and I certainly don’t want to setup a working mailer on mine. Besides, I might not even be connected to a network. And don’t even get me started on when you need to dry run bulk mailing.

Luckily, at least if you’re running *nix, there’s a very simple solution; replace your sendmail binary with the following shell script1:

<code>#!/bin/bash

LOGDIR="/tmp/sendmail-sim"
NOW=$(date +%Y-%m-%dT%H.%M.%S)
CNT=1
PRIVATELOG="$LOGDIR/$NOW.$CNT.log"
COMBINEDLOG="$LOGDIR/combined.log"

# If privatelogs are being used...
if [ ! -z "$PRIVATELOG" ]; then
    # ...make sure the filename is unique and create the file
    while [ -f $PRIVATELOG ]; do
        CNT=$(($CNT + 1))
        PRIVATELOG="$LOGDIR/$NOW.$CNT.log"
    done

    echo "$0 $*" > $PRIVATELOG
else
    # ...otherwise swap filenames
    PRIVATELOG=$COMBINEDLOG
    COMBINEDLOG=''
fi

echo "[$NOW]" >> $PRIVATELOG
while read BUF
do
    echo $BUF >> $PRIVATELOG
done

# Append privatelog to combinedlog when both logs are used
if [ ! -z "$COMBINEDLOG" ]; then
    echo "[$NOW]" >> $COMBINEDLOG
    cat $PRIVATELOG >> $COMBINEDLOG
fi

exit 0</code>

This will result in all input that is sent to sendmail being written to a separate2 log file in the /tmp/sendmail-sim directory, as well as a combined.log for convenient tailing.

Sorry, the comment form is closed at this time.

Meta

Pages

Search blog

Latest comments