Deprecated: Assigning the return value of new by reference is deprecated in /home/bigfish/blogs/adam/wp-includes/cache.php on line 36

Deprecated: Assigning the return value of new by reference is deprecated in /home/bigfish/blogs/adam/wp-includes/query.php on line 21

Deprecated: Assigning the return value of new by reference is deprecated in /home/bigfish/blogs/adam/wp-includes/theme.php on line 540
Setup a testing mail server using PHP on Mac OS X : Adam Royle

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?

  1. When an email is sent using PHP's mail() function, the email is piped through to the smtp_catcher.php script.
  2. The smtp_catcher.php script saves the email into a local folder as a .emlx file.
  3. The email is opened through Apple Mail and displayed on the screen (woo!).

Why should I use this?

  1. You don't need to change any of your application code. All emails will be routed through the smtp_catcher.php script and not sent through the interweb.
  2. 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.
  3. You will never mistakenly send an email out when testing your email sending code.
  4. 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

62 Responses to “Setup a testing mail server using PHP on Mac OS X”

  1. Helge on April 29th, 2010 7:05 am

    thanks a lot. nice for testing emails

  2. Jon Parker on September 8th, 2010 6:08 pm

    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

  3. Willem-Jan on December 16th, 2010 1:56 am

    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.

  4. wctube on October 11th, 2011 5:56 am

    It wouldn't work for me at first but when I removed the "sudo -u " from the sendmail_path, everything started working perfectly

  5. jmacNZ on November 2nd, 2011 10:04 am

    Thanks for the great tip, worked exactly as explained. nano obviously works ok as long as you follow the instructions EXACTLY.

    Thanks

  6. holger on November 12th, 2011 9:01 pm

    very cool solution. thanks a lot!

  7. oglomar on November 20th, 2011 7:29 pm

    really nice. thanks!

  8. barat on January 17th, 2012 9:37 pm

    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 :/

  9. Peter Cook on February 24th, 2012 3:19 am

    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.)

  10. chuck on March 7th, 2012 7:17 am

    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?

  11. hank on March 22nd, 2012 9:52 am

    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!

  12. Willem-Jan on March 22nd, 2012 9:58 am

    Are you editing the correct php.ini?
    See http://www.apachefriends.org/en/faq-xampp-windows.html#phpini for more info.

  13. Willem-Jan on March 22nd, 2012 9:59 am

    Sorry, didn't read correctly :-) The link won't help you, but you should still check if you adjusted the correct php.ini.

  14. Diogo Melo on July 18th, 2012 10:20 am

    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." :) :)

  15. Sue de Nimes on August 21st, 2012 7:34 am

    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

  16. Robert Abramski on August 29th, 2012 3:04 am

    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.

  17. Neelabh on September 9th, 2012 7:51 pm

    Helped alot.
    Thanks

  18. Sven on September 27th, 2012 5:10 am

    This is great! Very easy and handy.

  19. Sven on September 27th, 2012 5:33 am

    This is great! Very easy and handy.

  20. Summer on October 18th, 2012 5:28 pm

    GJ! save me lots of time, thanks!

  21. Simon B. on November 2nd, 2012 4:05 am

    This worked for me to, thanks a lot !

  22. Roman on January 9th, 2013 8:32 am

    Thank you man, smart solution, worked for me.

  23. pin on January 17th, 2013 6:51 pm

    happy

  24. pin on January 17th, 2013 6:53 pm

    happyccccccccccc

  25. Brian on January 19th, 2013 6:30 am

    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!!

  26. Brian on January 19th, 2013 10:58 pm

    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.

  27. Brian on January 20th, 2013 2:15 pm

    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!

  28. Fred on January 24th, 2013 1:57 am

    Followed every step on Lion, but no success ... :(

  29. Balu Ertl on February 13th, 2013 7:25 am

    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!

  30. Paul Mason on February 16th, 2013 10:57 am

    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.

  31. Buzzman on February 18th, 2013 6:44 am

    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!

  32. Chris Keane on February 22nd, 2013 7:59 am

    Thanks! This worked for me after I added write permissions to the smtp_out directory. (Thanks for that tip, Sue de Nimes)

  33. Konstantin on March 13th, 2013 3:13 am

    Great job man! Works awesome!

  34. sohbet on March 20th, 2013 9:56 am

    : 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!

  35. sohbet odaları on March 20th, 2013 9:56 am

    ipt change the file extension from .emlx to .eml, and assign Tb as default application. It worx like a charm!

  36. Jan-F on April 24th, 2013 9:21 am

    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

  37. Devil chom on June 29th, 2013 3:27 am

    Thank you so much.

    It did work without sudo -u username.. thanks.

  38. Devil chom on June 29th, 2013 3:51 am

    I think restarting the machine does the trick as well after following these instructions.

  39. Helen on July 4th, 2013 5:10 am

    this is my sendmail line in php.ini

    sendmail_path = php /Users//smtp_out/smtp_catcher.php

    note the php after the =

    this is what made it work

  40. Helen on July 4th, 2013 5:13 am

    something seems to have gone wrong with the path - I meant

    sendmail_path = php /Users//smtp_out/smtp_catcher.php

  41. Helen on July 4th, 2013 5:14 am

    it's still eating the line

    put your username between the double slash

  42. Dirk on July 13th, 2013 4:46 am

    Tnx, saved me a bunch.. (isp blocking html formatted mail for some reason.

  43. John Ballinger on August 1st, 2013 12:49 pm

    For me to get this working on OX 10.8

    // fixed write permissions to the smtp_out folder
    chmod o+w smtp_out

    // Removed the sudo from PHP.ini sendmail
    sendmail_path = /Users/johnb/smtp_out/smtp_catcher.php

    Otherwise worked a treat. Amazed.

  44. sohbet on August 6th, 2013 8:01 pm

    alayına isyan thanks you

  45. Karma on September 29th, 2013 3:35 am

    Very good trick! I confirm it works like a charm on mac os 10.8 without the sudo -u part and without the need of modifying the sudoers file! Enjoying it on AMPPS (if somebody is using it the working php.ini file is the one in /Applications/AMPPS/conf/ and depends on the version of php you have enabled 5.3 by default )
    P.S. to maintain my home clean rather than create the smtp_out folder i created .smtp_out ... just a tip! (then you have to update all the lines!)
    Thank you!

  46. Eric McConkie on November 7th, 2013 8:48 am

    rad! Thanks!

  47. Fexy on November 26th, 2013 11:24 pm

    Thanks a lot! You have no idea what have gone through. thanks a lot. Words cannot express my gratitude.

  48. Michael on November 29th, 2013 9:53 am

    It took a while before I realized, that I changed the wrong php.ini

    With the function phpinfo() I found the correct php.ini (Configuration File (php.ini) Path).

    I had to update the php.ini after I upgraded from OSX 10.7 (Lion) to 10.9 (Maverick)

  49. brexis on January 9th, 2014 9:21 am

    Thanks a lot..

  50. man on February 14th, 2014 8:25 pm

    """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"""

    i did exactly that and it works but i wanted to receive the mail on my mailbox not on another program

  51. man on February 14th, 2014 8:29 pm

    "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"

    i did exactly that and it works but i wanted to receive the mail on my mailbox not on another program

  52. Rob Hidalgo on February 22nd, 2014 6:37 am

    Awesome tutorial and I can confirm that this works perfectly on 10.9 Mavericks. Like many other posters I had to remove the sudo stuff, but otherwise it works great!

  53. Jordan on April 23rd, 2014 2:38 am

    Glad this tutorial is still useful to myself and others 5 years after it was written!

    I followed the instructions as written, and had success in Mavericks 10.9.2

    The one step I did not see was exiting visudo: Hit escape, type :wq, then hit enter. http://stackoverflow.com/questions/12736351/exit-save-edit-to-sudoers-file-putty-ssh

  54. fasdfadsf on May 4th, 2014 2:57 am

    adsfadsfadsf

  55. develko on June 12th, 2014 4:57 am

    Also may be using FakeSMTP for testing
    http://fakesmtp.ru/

  56. LittleHoopoe on July 8th, 2014 1:16 am

    thanks worked fine for me!!!

Leave a Reply