How To Log Emails Sent With PHP's mail() Function To Detect Form Spam
|
Submitted by till (Contact Author) (Forums) on Wed, 2009-08-05 17:41. :: Debian | Apache | Email | PHP | Security
How To Log Emails Sent With PHP's mail() Function To Detect Form SpamVersion 1.0 If you are running a webserver you might have faced the problem already: somewhere on your server is a vulnerable contact form or CMS system written in PHP that gets abused by spammers to send emails trough your server. If you have more than a few websites, it is a pain to detect which of the sites is vulnerable and sends the spam emails. This tutorial explains the installation of a small wrapper script which logs email messages sent trough the PHP mail() function. I'm using Debian Linux here for this tutorial but the script should work on any Linux distribution.
1 Installing the wrapper scriptOpen a new file /usr/local/bin/phpsendmail... vi /usr/local/bin/phpsendmail ... and insert the following script code: #!/usr/bin/php If you use a different Linux distribution than Debian, the sendmail binary might be in a different location than /usr/sbin/sendmail and you have to change the sendmail path in the line $sendmail_bin = '/usr/sbin/sendmail'; of the script. Now make the script executable... chmod +x /usr/local/bin/phpsendmail ... and create the logfile and make it writable: touch /var/log/mail.form
2 Modifying the php.iniNow we reconfigure PHP so that it uses our wrapper script to send the emails. Open the php.ini file... vi /etc/php5/apache2/php.ini ... and change the lines... [mail function] ; For Win32 only. SMTP = localhost smtp_port = 25 ; For Win32 only. ;sendmail_from = me@example.com ; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). ;sendmail_path = ... to: [mail function] ; For Win32 only. ;SMTP = localhost ;smtp_port = 25 ; For Win32 only. ;sendmail_from = me@example.com ; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). sendmail_path = /usr/local/bin/phpsendmail If you use php as cgi, with suphp or as fcgi, then change the same lines in the file /etc/php5/cgi/php.ini, too. Restart the Apache webserver to apply the changes. /etc/init.d/apache2 restart
3 Test the setupTo test this setup, create a new php file with the name mailtest.php in one of your websites with the content: <?php
mail('yourname@yourdomain.com','This is a test message subject','This is a test message body');
echo 'Mail sent.';
?>
Then open the file in a webbrowser to execute it. The test message should be logged now into the logfile. Check this with the command: cat /var/log/mail.form
|
www.seamlessenterprise.com
One number. One voicemail. Seize the lead. Sprint Mobile Integration.
www.seamlessenterprise.com
One Number. One Voicemail.
Make it easier for clients to reach you. Turn your desk phone and mobile phone into one with Sprint Mobile Integration.
www.seamlessenterprise.com
One number. One voicemail. Sprint Mobile Integration.
www.seamlessenterprise.com
AT&T Synaptic Compute as a Service. Boost your power on demand.
Trial: IBM Cognos Express Reporting, Analysis & Planning
Learn benefits of Simpana software.
View the Gartner Video







Recent comments
14 hours 16 min ago
15 hours 10 min ago
18 hours 32 min ago
23 hours 52 min ago
1 day 16 min ago
1 day 7 hours ago
1 day 8 hours ago
1 day 8 hours ago
1 day 10 hours ago
1 day 12 hours ago