Monday, April 15, 2024
HomeDeveloperHow to catch emails sent with PHP on your local server

How to catch emails sent with PHP on your local server


Related stories

Unleash Your Luck: Togel Rakyat Slot Game Extravaganza

Introduction Welcome to the ultimate gaming experience where luck meets...

Placing Bets Stateside: A Guide to Online Casinos in the USA

In the ever-evolving landscape of online entertainment, online casinos...

KK Bandar’s Poker Paradise: Play, Win, Repeat

Poker has long been hailed as the quintessential card...

Your One-Stop Shop for Global Communication: Translation Companies in the UK

In an increasingly interconnected world, businesses are expanding their...

From Novice to Pro: Navigating Slot Gacor and Online Gaming Like a Champ

In the vast landscape of online gaming, mastering the...

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:

$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