/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:

#!/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

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.

1 It might be a good idea to rename your original sendmail binary instead of replacing it entirely, in case you need to recover it at a later time. On another note, it might be a good idea to just symlink the replacement script from another file to prevent it from being overwritten on a system update.

2 If you want just the combined.log uncomment PRIVATELOG on line 6.

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Meta

Pages

Search blog

Latest comments