Wednesday, June 18, 2025
HomeBusinessHow to catch emails sent with PHP on your local server

How to catch emails sent with PHP on your local server

Date:

Related stories

Exploring scr66 Casino Australia’s Bonuses

scr66 Casino Australia is renowned not only for its...

Fast & Easy: Register Legit99 in 3 Steps

Introduction to Legit99 Legit99 is emerging as a trusted and...

Expert Picks: Best Odds in Online Sportsbooks

Understanding the Value of Great Odds In the world of...

Essex Soft Play Bus – Hire for 4–8‑Year‑Olds

A Unique Party Experience for Active Young Minds When it...

Experience the Best Pokies with MethKing Australia

In the ever-growing world of online gaming, pokies (or...
spot_img

First, create a folder for your outgoing mail. We will create it in under /var/log/ where all the logs are stored:

$ sudo mkdir /var/log/mail

Create file /usr/local/bin/sendmail using your favorite text editor, I normally use Nano editor:

$ sudo nano /usr/local/bin/sendmail

Add following PHP script to this new “sendmail” file:

#!/usr/bin/php
<?php
$input = file_get_contents('php://stdin');
preg_match('|^To: (.*)|', $input, $matches);
$filename = tempnam('/var/log/mail', $matches[1] . '.');
file_put_contents($filename, $input);

This is where the magic happens and emails sent from PHP are stored as textfiles. The email address is extracted from the full email using regular expression, and used as the filename base – the tempnam() function creates a random filename with the email address used as filename prefix. You can alter this part however you want, for example include a timestamp in the filename.

We need to link our PHP script to PHP’s sendmail functionality. Edit your php.ini file and set the sendmail_path setting as following:

sendmail_path = /usr/local/bin/sendmail

If you use Ubuntu/Kubuntu 12.4, your php.ini file is normally located at /etc/php5/apache2/php.ini. You can find your php.ini location also using the phpinfo() function, where you look for “Loaded Configuration File” value in the function’s output.

Now we need to set permissions for new files/folders:

$ sudo chmod 755 /usr/local/bin/sendmail
$ sudo chmod 777 /var/log/mail

Restart apache:

$ sudo /etc/init.d/apache2 restart

And that’s it! You can now try to send an email using PHP’s mail() function and check the /var/log/mail folder.

Latest stories

spot_img