Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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);
share|improve this answer

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.

share|improve this answer
    
You might want to change !== false to !== "" or != false to avoid the script hanging forever; I had forgotten that fread() works differently from fgets(). –  grawity Jan 19 '12 at 23:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.