How to log specific sSMTP errors

last updated July 28, 2021 6:37 UTC

The sSMTP mail server has, in my experience, proven to be a surprisingly performant tool. In addition, I use the WP Mail SMTP plugin, which covers most scenarios regarding the email logging needs. However, there was not enough information available in some exceptional cases (email provider down, IP address ban, non-WP emails etc.).

The fact is the sSMTP on its own can log everything (including the email content) into the syslog. Such logging seemed excessive, though, so I created a wrapper script that filters only specific failures and saves them into files for further processing:

#!/bin/sh
set -e
LOGFILE="/tmp/ssmtp-$(date +%Y%m%d-%H%M%S)"
echo "$0 $@" > "$LOGFILE"
echo "" >> "$LOGFILE"
exec 2>${LOGFILE}
tee -a "$LOGFILE" | /usr/sbin/ssmtp "$@"
exec 2>&-
if [[ ! $LOGFILE == /ssmtp:/* ]]; then
  rm -rf $LOGFILE
fi

To use the wrapper, you need to save it as a script and make it executable. Then point the email config to it. In my case, I had to change a line in the php.ini as below:

sendmail_path = /usr/sbin/myssmtp.sh -t -C /ssmtp.conf

And finally, processing the outputs can be done (for example) like this:

if ls /tmp/ssmtp-* 1> /dev/null 2>&1; then
   tail -n +1 /tmp/ssmtp-*;
   rm -rf /tmp/ssmtp-*;
fi
Shopping Cart
There are no products in the cart!
Total
 0.00
0