Log mails send via php mail function

Here is a simple script which can log necessary information regarding mails send via php mail() function.

Create a file /usr/local/bin/sendmail-logger with the following code inside and correct the file ownership/permission to root/755

 
logger -p mail.info sendmail-logger: site=${HTTP_HOST}, 
client=${REMOTE_ADDR}, script=${SCRIPT_NAME}, pwd=${PWD}, uid=${UID}, 
user=$(whoami) 
/usr/sbin/sendmail -t -i $*

Create a file /usr/local/bin/php_set_envs.php with the following code inside

<? 
putenv("HTTP_HOST=".@$_SERVER["HTTP_HOST"]); 
putenv("SCRIPT_NAME=".@$_SERVER["SCRIPT_NAME"]); 
putenv("SCRIPT_FILENAME=".@$_SERVER["SCRIPT_FILENAME"]); 
putenv("DOCUMENT_ROOT=".@$_SERVER["DOCUMENT_ROOT"]); 
putenv("REMOTE_ADDR=".@$_SERVER["REMOTE_ADDR"]); 
?>

Edit the following values in php.ini

sendmail_path = /usr/local/bin/sendmail-logger 
auto_prepend_file = /usr/local/bin/php_set_envs.php

The log information will be written on /var/log/maillog

root@ [~]# grep sendmail-logger /var/log/maillog 
Nov 26 04:47:03 cpanel1 logger: sendmail-logger: site=11.22.33.44, 
client=11.11.11.11, script=/testmail.php, pwd=/usr/local/apache/htdocs, 
uid=99, user=nobody
Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: