Setup a testing mail server using PHP on Mac OS X
You may already know this feeling. You've spent a few hours developing a shell script to send out billing emails to customers and after pressing "enter" you suddenly realise that your script is sending out hundreds of dummy emails to your customers (you had copied some data from the live server "just to test things out a bit").
You quickly stop the script, but the damage is already done! You're now getting confused emails from your customers... time to start writing that apology email!....... OR... you could implement a testing mail server so this situation NEVER HAPPENS AGAIN!
How does it work?
- When an email is sent using PHP's
mail()function, the email is piped through to thesmtp_catcher.phpscript. - The
smtp_catcher.phpscript saves the email into a local folder as a.emlxfile. - The email is opened through Apple Mail and displayed on the screen (woo!).
Why should I use this?
- You don't need to change any of your application code. All emails will be routed through the
smtp_catcher.phpscript and not sent through the interweb. - You can preview exactly how your email will look when it's received by your users, including the original recipients "To" address, any attachments and email headers.
- You will never mistakenly send an email out when testing your email sending code.
- Emails are saved and opened instantly, so no waiting for your email to travel through multiple mail servers to arrive in your inbox.
How do I install it?
Open a terminal window and copy and paste the following...
cd ~/
mkdir smtp_out
cd smtp_out
curl -o smtp_catcher.php http://blogs.bigfish.tv/adam/examples/testing-mail-server/smtp_catcher.php.txt
chmod +x smtp_catcher.php
Now you've got the output folder created and the smtp_catcher.php script installed, you just need to update your php.ini and set some permissions.
NB. If your php binary is not in /usr/bin/php then you will need to edit the first line of smtp_catcher.php.
Configure PHP to pipe emails to the smtp_catcher.php script
Open up your php.ini file and find the following line. Please note, if you have a separate php.ini file for your CLI binary, you'll need to edit that one as well.
;sendmail_path =
Change it to the following (replacing "<your_username>" with your Mac OS X username).
sendmail_path = sudo -u <your_username> /Users/<your_username>/smtp_out/smtp_catcher.php
Save your changes and restart your webserver.
Give permission to PHP to open Apple Mail
PHP runs as the www user by default, which will mean smtp_catcher.php won't be able to open your GUI mail program, so we have to give sudo access to the www group.
sudo nano /private/etc/sudoers
Add the following line to the sudoers file and save (replacing "<your_username>" with... yep, your username).
%www ALL=(ALL) NOPASSWD: /Users/<your_username>/smtp_out/smtp_catcher.php
This will now give permission to PHP to execute the smtp_catcher.php script with sudo permissions, without having to enter a password!
Test it to make sure everything works
The final step is to create and execute a test script, which can be as simple as this.
<?php
mail('john.doe@example.com', 'The Magical Subject Line', 'The Magical Message Body');
?>
What if I'm using PHP on Windows?
Easy! Install the Test Mail Server Tool and uncomment the following lines from your php.ini, and restart your webserver.
SMTP = localhost
smtp_port = 25
Comments
11 Responses to “Setup a testing mail server using PHP on Mac OS X”
Leave a Reply
thanks a lot. nice for testing emails
Thanks very much for such a nice and simple solution - especially with the cut and paste code
It wouldn't work for me at first but when I removed the "sudo -u " from the sendmail_path, everything started working perfectly
Thanks again
Thanks for the tip! Very useful!
One thing you really need to change:
Never edit the sudoers file with nano.
Use visudo for this purpose. Else, if you make a mistake, you won't be able to use sudo anymore. That includes using sudo to edit the sudoers file!
What visudo does for you, is check the syntax in the file before actually saving it in the sudoers file.
If you hate Vi and want to use the nano editor. Use the following command:
EDITOR=/usr/bin/nano sudo visudo
With that command you edit the sudoers file safely with the nano editor.
It wouldn't work for me at first but when I removed the "sudo -u " from the sendmail_path, everything started working perfectly
Thanks for the great tip, worked exactly as explained. nano obviously works ok as long as you follow the instructions EXACTLY.
Thanks
very cool solution. thanks a lot!
really nice. thanks!
Made exactly like shown here but ... it is not working - no action, no even mails in smtp_out directory ... no nothing :(
I tried with sudo -u , without it (only path to script), tried in php.ini in xampp, in smtp_catcher.php i tried with:
#!/usr/bin/php
in first line, and with:
#!/Applications/XAMPP/xamppfiles/bin/php
I'm confused ... mac shoud be so easy system ... on windows i just had to lounch Papercut mail and everything worked ootb :/
Now you've got the output folder created and the smtp_catcher.php script installed, you just need to update your php.ini and set some permissions.