Search code examples
emailxamppmail-server

Intercept all emails sent from Xampp Mercury Mail Server


I have a Xampp Server that I use only in a development environment. In order to preview emails that would be sent from the live sites without actually sending them I would like to intercept all the emails sent from this server. I would like to be able to either send them all to a specific email or save them as files instead of sending them to whatever address they're set to go to. This way I can make sure they are correct without accidently sending emails during testing.


I found a similar question with an answer here but was unable to find a way to open any of the dialogs in the answer and so it didn't get me very far.


Thanks in advance for your help!

Solution

  • You can accomplish this using the sendmail configuration within your php.ini.

    Create a file named smtp_catcher.php and the set the sendmail_path

    sendmail_path = "php C:\path\to\file\smtp_catcher.php"
    

    Then in your smtp_catcher.php add this block:

    #!/Applications/XAMPP/xamppfiles/bin
    <?php
    
    # create a filename for the emlx file
    list($ms, $time) = explode(' ', microtime());
    $filename = dirname(__FILE__).'/'.date('Y-m-d h.i.s,', $time).substr($ms,2,3).'.emlx';
    
    # write the email contents to the file
    $email_contents = fopen('php://stdin', 'r');
    $fstat = fstat($email_contents);
    file_put_contents($filename, $fstat['size']."\n");
    file_put_contents($filename, $email_contents, FILE_APPEND);
    
    # open up the emlx file (using Apple Mail)
    exec('open '.escapeshellarg($filename));
    
    ?>
    

    Now I am not sure what extension you'll need to use to view the emails but this should catch all emails going out.

    NOTE: make sure that php is in your window's environment PATH