In this post I’m going to talk about installing Mailcatcher for PHP on Mac OSX. It is particularly useful when developing new websites as you can test emails without worrying if they are going out to a recipient.
This has two advantages:
- You can rapidly test emails without worrying that it will go out to it’s recipient
- You can save money on services such as postmark or mailchimp since development emails no longer will hit that service.
Installing Mailcatcher
Mac’s come preinstalled with Ruby so it’s as simple as installing a ruby gem. This is just a package manager which allows you to easily install programs and libraries. To install mailcatcher simply run the following.
1 |
sudo gem install mailcatcher |
Let it do it’s thing which will involve installed any dependencies for mailcatcher and finish setting up.
One completed successfully you can run the following:
1 |
mailcatcher |
If the program started successfully you will get the following:
1 2 3 4 |
Starting MailCatcher ==> smtp://127.0.0.1:1025 ==> http://127.0.0.1:1080 *** MailCatcher runs as a daemon by default. Go to the web interface to quit. |
If you open http://127.0.0.1:1080 you will be presented with the mailcatcher interface.
That’s the first part and mailcatcher is now setup for the mac. Next we need to link it with php, which is covered in the next section.
Setting Mailcatcher up with PHP
Once this has been setup we now need to link it to PHP.
First we need to find out where catchmail (a program installed with mailcatcher) has installed to. I’m going to assume it’ll be located in the following directory:
1 |
/usr/local/bin/catchmail |
However if you are unsure you can run the following command:
1 |
which catchmail |
This will return the directory path for catchmail. We will need this later.
Next we need to find the php.ini file so that we can configure catchmail to respond to the mail() command in php. You can find this by running
1 |
php -i |
And finding the configuration file path function. e.g.
1 |
Configuration File (php.ini) Path => /usr/local/etc/php/7.1 |
Once found open up the file using your favourite command line text editor:
1 |
sudo nano /usr/local/etc/php/7.1 |
Update the following:
1 |
smtp_port = 25 |
to
1 |
smtp_port = 1025 |
And update:
1 |
;sendmail_path = |
to
1 |
sendmail_path = /usr/local/bin/catchmail |
Once the above has been done save the file and quit out.
Restart php locally and try to send a test email using the mail() command.
If you open up the following url: http://127.0.0.1:1080
You should now see your email going into Mailcatcher.
Congratulations you now have a working setup for testing development emails. Good job!
Hello
Thank you for the document it’s simple and working well.
I only have one problem. Mailcatcher puts subject in the “From” field and From in the “Subject” field.
How is it possible ?
How can i correct it ?
Thank you for your answer.
Best regards
Hi Bernard,
Thanks for your comment. May I ask how you are sending the email out? There could be a configuration issue with the module or the function you are using.
Thanks,
Jamie