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.
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:
mailcatcher
If the program started successfully you will get the following:
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:
/usr/local/bin/catchmail
However if you are unsure you can run the following command:
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
php -i
And finding the configuration file path function. e.g.
Configuration File (php.ini) Path => /usr/local/etc/php/7.1
Once found open up the file using your favourite command line text editor:
sudo nano /usr/local/etc/php/7.1
Update the following:
smtp_port = 25
to
smtp_port = 1025
And update:
;sendmail_path =
to
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!