Skip to content

Instantly share code, notes, and snippets.

@superjojo140
Last active May 11, 2023 13:12
Show Gist options
  • Save superjojo140/b617ca0d054c3c3e87e26b37e4666efb to your computer and use it in GitHub Desktop.
Save superjojo140/b617ca0d054c3c3e87e26b37e4666efb to your computer and use it in GitHub Desktop.
Log mails send with php's mail() function
  1. Create a folder to store the script and the mail logs.
mkdir /somewhere/phpmail
mkdir /somewhere/phpmail/mails
  1. Place the logmail.sh script in the phpmail folder
  2. Make sure the php process has write access to this folder:
chmod -R 777 /somewhere/phpmail
  1. Open the php.ini config file and edit the sendmail_path parameter:

On archlinux: /etc/php/php.ini

sendmail_path = "/somewhere/phpmail/logmail.sh"
  1. If running php via webserver, you have to restart the webserver.
#!/bin/bash
UUID=`uuidgen`
UUID=${UUID:0:5}
CURRENTDATE=`date +"%Y-%m-%d %T"`
CURRENTFOLDER=`dirname "$(realpath $0)"`
MAILPATH="$CURRENTFOLDER/mails/mail $CURRENTDATE $UUID.eml"
echo Save mail to $MAILPATH
cat > "$MAILPATH"
#Delete all files in /mails folder that are older than 1 day (-mtime +1)
find "$CURRENTFOLDER/mails/" -mindepth 1 -mtime +1 -delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment