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. If you are not using the Apache/PHP that comes pre-installed in Mac OS X, then you may need to change "www" to suit your needs.
sudo visudo
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
40 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 :/
Thanks very much for this.
I did every step up to and including 'Configure PHP to pipe emails to the smtp_catcher.php script'.
I removed the sudo stuff from sendmail_path, as others have suggested. I also ran:
chmod +x smtp_catcher.php
and it all worked sweetly.
(I haven't bothered with the Apple Mail side of things, I just check in the directory.)
This looks hopeful for a solution testing php on my ubuntu localhost. There are 40 different recipes out there but this one is very nicely written up.
OSX is like FreeBSD isn't it?
I'm not using XAMPP, but I am having similar problems to Barat. I've followed all of the instructions above, include with "sudo -u" and without, and I do not see any emails (or any files for that matter) created in the smtp_out folder.
Thanks for the tutorial, and any help in getting this work would be greatly appreciated!
Are you editing the correct php.ini?
See http://www.apachefriends.org/en/faq-xampp-windows.html#phpini for more info.
Sorry, didn't read correctly :-) The link won't help you, but you should still check if you adjusted the correct php.ini.
works perfect! thank you!
don't forget to remove the "" when typing your username:
%www ALL=(ALL) NOPASSWD: /Users//smtp_out/smtp_catcher.php
should Be
%www ALL=(ALL) NOPASSWD: /Users/Peter/smtp_out/smtp_catcher.php
"replacing "" with... yep, your username." :) :)
If you are still having problems, check the permissions of the folder you are trying to write emails to.
In terminal, navigate to the smtp_out enclosing folder, and add write permissions for others:
cd /Users//
chmod o+w smtp_out
I've been looking for a simple solution to this for a long time. Trying to depend on mail servers with spam filters is not ideal for testing. I'm glad I can finally handle this locally.
Helped alot.
Thanks
This is great! Very easy and handy.
This is great! Very easy and handy.
GJ! save me lots of time, thanks!
This worked for me to, thanks a lot !
Thank you man, smart solution, worked for me.
happy
happyccccccccccc
I am working within MAMP, I have verified that the php binary is in /usr/bin/php (via terminal) and made sure that I am editing the correct php.ini. file, but to no avail. Does anyone know if the default www group within MAMP uses something other than www (I tried www-data as well). I'm not too comfortable within terminal, but if someone can help me walk through the steps to give permissions to the smtp_catcher.php folder I would be so grateful. Any other tips would be greatly appreciated!!
After a little investigating, I found that I do have permissions given to the smtp_catcher.php folder. Also, when executing a test script, I just get a blank page in the browser window. It seems as thought the script stops at the [mail function], I've tried with sudo -u and without. Any help would be appreciated.
Wow, finally got it by reinstalling OS. I obviously really wanted this and it works great! Took less than 5 minutes this time. Be careful in the terminal!! Thanks a ton for this easy solution!
Followed every step on Lion, but no success ... :(
Hi Adam,
It's a great idea from you, and thank you for sharing with us.
+1 tip: if you'd love OS Thunderbird instead of Apple's Mail, in the PHP script change the file extension from .emlx to .eml, and assign Tb as default application. It worx like a charm!
Thanks, works well!
I did stuff up my visudo file and couldn't get back in until i restored it from time machine. I think it's a good idea to have a back up before messing around with it.
Hi,
On phpinfo(); data on my local server I see next settings:
Configuration File (php.ini) Path /Applications/MAMP/bin/php/php5.4.4/conf
Loaded Configuration File
/Library/Application Support/appsolute/MAMP PRO/conf/php.ini
But if I change 'sendmail_path' settings, they dont change. I always see only 'sendmail_path /usr/sbin/sendmail -t -i' =)
Help my, please!
Thanks! This worked for me after I added write permissions to the smtp_out directory. (Thanks for that tip, Sue de Nimes)
Great job man! Works awesome!
: if you'd love OS Thunderbird instead of Apple's Mail, in the PHP script change the file extension from .emlx to .eml, and assign Tb as default application. It worx like a charm!
ipt change the file extension from .emlx to .eml, and assign Tb as default application. It worx like a charm!
Thanks a lot for sharing these steps! A local testing mail server is exactly what I – and obviously many others – desire.
I would be glad if it ran on my Mac OS X 10.8.3 (Mountain Lion) with XAMPP server 1.7.3 manually installed (PHP version 5.3.1), but unfortunately it does not.
Here is what I did:
1. Installing smtp_catcher.php by pasting the given commands into terminal window.
2. Setting read and write permissions of the folder smtp_out for My_User.
3. Changing the first line of smtp_catcher.php to #!/Applications/XAMPP/xamppfiles/etc which is the path to my php.ini taken from phpinfo().
4. Setting sendmail_path = sudo -u My_User /Users/My_User/smtp_out/smtp_catcher.php in php.ini file.
5. Giving sudo access to XAMPP server (started as user and group 'nobody') by adding the two lines
'nobody ALL=(ALL) NOPASSWD: /Users/My_User/smtp_out/smtp_catcher.php' and
'%nobody ALL=(ALL) NOPASSWD: /Users/My_User/smtp_out/smtp_catcher.php'.
Results:
When running the test script on my local machine – no reaction from Apple Mail and no file in smtp_out.
But if I call sudo -u My_User php ./smtp_catcher.php in terminal window an .emlx file is being created.
I suspected a permission issue and thus tried several combinations with or without 'sudo -u My_User', different users and different user settings for XAMPP server. Right now I have no clue.
Any ideas are welcome!
Thanks and regard, Jan-F