In PHP you can send email with the PHP mail() function.

With Linux systems, a sendmail program is included to send mail, but let's face it, most of us prefer to use Windows on a home computer. Anyway, port 25 (for sending email) is probably blocked by your ISP.

So what are we to do if we want to use the mail() function in our test environment?

Well, we can specify another program to run for sending email, but this program doesn't have to send, it can simply save the data to a log file.

In the php.ini file, we can specify sendmail_path = "php C:\htdocs\scripts\mail\mail.php"

This will run a php script called mail.php that you can write yourself.

The actual email header data is sent to this script as STDIN data like you might type at the command line which makes it a little awkward to capture in a PHP script.

Anyway, here is the code that I wrote for mail.php:

PHP Code:
<?php
$headers 
'Headers received at: ' date("H:i:s"). "\n\n";
foreach(
$_SERVER as $name => $value) {    $headers .= "$name => $value\n";}
foreach(
$argv as $name => $value) {    $headers .= "$name => $value\n";}
while (
FALSE !== ($line fgets(STDIN))) {   $headers .= $line;}
file_put_contents(dirname(__FILE__) . '/headers.txt'$headers);
echo 
"**** The mail.php script has finished running. ****";
This script grabs and saves all the interesting data sent by the mail() function to the server.

Some more useful lines to add to php.ini in the [mail function] settings:

; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header = On


; The path to a log file that will log all mail() calls. Log entries include
; the full path of the script, line number, To address and headers.
mail.log = "C:\htdocs\scripts\mail\log.txt"


Here is the full php.ini code entry that I ended up with in the mail area:

Code:
[mail function]
; For Win32 only.
; http://php.net/smtp
; SMTP = localhost
; http://php.net/smtp-port
; smtp_port = 25


; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = me@example.com


; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path = "php C:\htdocs\scripts\mail\mail.php"


; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
; mail.force_extra_parameters = xyzdgtrhew


; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header = On


; The path to a log file that will log all mail() calls. Log entries include
; the full path of the script, line number, To address and headers.
mail.log = "C:\htdocs\scripts\mail\log.txt"
Unfortunately, I couldn't work out an easy way to identify which particular virtual host would be sending the email, only the name of the script from the mail.add_x_header = On setting.