Looking for a PHP mail intercepting application

0

I have PHP (XAMPP) set up locally on a Windows OS, and I am writing a webapp with it. This webapp send out several emails. For testing purposes I do not actually wish to forward mails generated by the system.

I also don't want to setup mercury and pegasus mail, as the only functionality I need is an application that will show me every email sent and allows me to click possible links in the emails. Nothing more is needed from the app. I don't actually need to store the emails sent out, and I don't want to be limited to using only @localhost email addresses.

To do this I assume that I need to configure PHP.ini's setting: 'sendmail_path' to an application that will intercept the mails and display them to me.

Is there such an application, and where could I find it?

Michael Frey

Posted 2011-10-07T17:18:39.587

Reputation: 111

Answers

2

The sendmail interface became popular due to its simplicity: the message including headers is simply written to its stdin. This means that you can just write a program that copies its stdin to a file.

Assuming the command-line PHP is included with XAMPP, you could use php.exe fake-sendmail.php as the sendmail_path, with fake-sendmail.php containing the following script.

<?php
$name = time() . ".eml";
$fh = fopen($name, "w");
if (!$fh) die;
while ($buf = fread(STDIN, 8192))
    fwrite($fh, $buf);
fclose($fh);

user1686

Posted 2011-10-07T17:18:39.587

Reputation: 283 655

1

I tryed the solution posted by grawity and it works. Here'a modifyed version of the code so that you can have the eml files in the same folder as the php file whatever happens :

<?php
$name = dirname(__FILE__).'/'.time() . ".eml";
$fh = fopen($name, "w");
if (!$fh) die;
for($i = 0; $i < $argc; $i++)
    fwrite($fh, 'Arg'.$i.': '.$argv[$i].PHP_EOL);
while (($buf = fread(STDIN, 8192)) != "")
    fwrite($fh, $buf);
fclose($fh);

Edit: I also added 2 lines to print all parameters sent to the command line. Finaly, I now handle the return value of fread properly thanks to grawity.

Frédéric Olivier

Posted 2011-10-07T17:18:39.587

Reputation: 11

You might want to change !== false to !== "" or != false to avoid the script hanging forever; I had forgotten that fread() works differently from fgets(). – user1686 – 2012-01-19T23:47:11.043