Skip to content

Instantly share code, notes, and snippets.

@alchemycs
Created February 25, 2014 04:58
Show Gist options
  • Save alchemycs/9202982 to your computer and use it in GitHub Desktop.
Save alchemycs/9202982 to your computer and use it in GitHub Desktop.
Don't accidentally send emails when testing email function from PHP (*nix including OS X)
#!/bin/bash
#
# 1. Put this somewhere like /usr/local/bin/fakemail.sh
# 2. Set the permissions with: chmod a+x /usr/local/bin/fakemail.sh
# 3. Edit your php.ini and set: sendmail_path=/usr/local/bin/fakemail.sh
#
# Now your email will not be sent but logged to unique files that are timestamped.
# We use $RANDOM just to try and reduce collissions in case multiple emails are sent at the same time.
#
OUTDIR=/tmp/fakemail
mkdir -p $OUTDIR
OUTFILE=$OUTDIR/fakemail-$(date +%Y%m%d%H%M%S)-$RANDOM.txt
echo "CLI: $@" > $OUTFILE
while read line; do
echo "Line: $line" >> $OUTFILE
done;
@alchemycs
Copy link
Author

This is a quick and dirty solution that ensures you don't accidentally send emails while testing. Particularly good for when doing bulk emails.

If you want to actually see the contents in an email client you are probably better to configure postfix with a re-write rule that sends everything to a specific testing email address.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment