sSMTP doesn't provide detailed email logging by default. Many developers need to capture complete email data (headers, body, recipients) for debugging or auditing purposes when using this lightweight MTA.
The most reliable method is creating a wrapper script that:
#!/bin/bash
# Path to original sSMTP binary
SSMTP_BIN="/usr/sbin/ssmtp"
# Log file location
LOG_FILE="/var/log/ssmtp_full.log"
# Capture all input
INPUT=$(cat)
# Log everything with timestamp
{
echo "====== $(date) ======"
echo "Recipients: $@"
echo "Headers and Content:"
echo "$INPUT"
echo ""
} >> "$LOG_FILE"
# Pass through to real sSMTP
echo "$INPUT" | $SSMTP_BIN "$@"
For a lighter solution that doesn't require script maintenance:
#!/bin/bash
cat | tee -a /var/log/ssmtp_full.log | /usr/sbin/ssmtp "$@"
After creating the wrapper:
- Make it executable:
chmod +x /usr/local/bin/ssmtp_wrapper
- Configure applications to use the wrapper path instead of direct sSMTP
- Set appropriate permissions:
chown root:mail /var/log/ssmtp_full.log
- Consider log rotation for the log file
For structured JSON logging (requires jq):
#!/bin/bash
LOG_FILE="/var/log/ssmtp_structured.log"
TIMESTAMP=$(date -Iseconds)
INPUT=$(cat)
jq -n \
--arg ts "$TIMESTAMP" \
--arg recipients "$*" \
--arg content "$INPUT" \
'{timestamp: $ts, recipients: $recipients, content: $content}' \
>> "$LOG_FILE"
echo "$INPUT" | /usr/sbin/ssmtp "$@"
When debugging email delivery issues or maintaining audit trails, logging complete email transactions through sSMTP becomes crucial. The default configuration often doesn't provide sufficient detail about sent messages.
Creating a wrapper script that intercepts calls to ssmtp allows comprehensive logging while maintaining original functionality. Here's a robust implementation:
#!/bin/bash
# sSMTP wrapper script for complete email logging
LOG_FILE="/var/log/ssmtp_wrapper.log"
TIMESTAMP=$(date +"%Y-%m-%d %T")
{
echo "===== Email Transaction Start [${TIMESTAMP}] ====="
echo "Command invoked: $0 $@"
echo "Environment:"
printenv | grep -i 'mail\|ssmtp'
echo "---- Headers and Content ----"
cat /dev/stdin
echo "===== Email Transaction End ====="
echo ""
} >> "$LOG_FILE" 2>&1
# Pass through to original ssmtp
exec /usr/sbin/ssmtp "$@"
- Save the script as
/usr/local/bin/ssmtp_wrapper
- Make it executable:
chmod +x /usr/local/bin/ssmtp_wrapper
- Configure applications to use the wrapper instead of direct ssmtp
- Set up log rotation for the log file
For production environments, consider these enhancements:
# Add to wrapper script for better parsing:
echo "Process Info:"
echo "PID: $$"
echo "Parent PID: $PPID"
echo "User: $(id -un)"
echo "Host: $(hostname)"
# For sensitive environments, add redaction:
sed -r 's/(password|auth)=[^ ]+/REDACTED/g' | tee -a "$LOG_FILE"
Create /etc/logrotate.d/ssmtp_wrapper
:
/var/log/ssmtp_wrapper.log {
daily
missingok
rotate 30
compress
delaycompress
notifempty
create 640 root adm
}
- Permission errors: Ensure the wrapper and log file have proper permissions
- Missing content: Verify stdin is properly captured
- Encoding problems: Add
LC_ALL=C
to the script for consistent character handling