Top ↑

Run a local mailserver for PHP

If you web develop a lot, you'll develop locally because it's fast. One of the few problems with developing locally comes when you want to send email - your computer won't be set up for it, and most likely won't have a mailserver actually installed.

Using a nice little mailserver called msmtp, I'll be showing you how to rememdy this gap in functionality. What we will be doing is setting up your GMail account to send emails from your local server, so any email you send will come from the GMail address you provide.

Install

This is really easy. Just run the following command (for Ubuntu and other Debian derivatives):

sudo apt-get install msmtp ca-certificates

apt will resolve dependencies and install the program. ca-certificates is needed to authenticate with GMail.

Configuration

I use a GMail account, so we'll be configuring msmtp for one of those. I'm guessing other mail providers will have a similar configuration - host, port, etc will need changing.

Below is the config file I use. This will have to go in /etc/msmtprc (create it if it doesn't exist already).

defaults
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt

account default
host smtp.gmail.com
port 587
auth on 
user purplemoose@gmail.com
password secretpa5s
from purplemoose@gmail.com
logfile /var/log/msmtp.log

The important bits to note here is the host, port, user, password and from. What each parameter does should be fairly apparent, but I'll do a quick run through of each of them anyway

  • account default means the default account.
  • host tells msmtp what server to use to transfer the emails around.
  • port defines what port to use for GMail's server. Leave it at 587 for GMail. Other providers will vary.
  • auth on means a username and password is supplied, as GMail asks for one.
  • user and password should be your email address and password for logging into GMail.
  • from purplemoose@gmail.com should be the same email address you use to log in.

The final line is obvious in it's function and shouldn't need explaining. Once you've configured msmtp properly, change the file permissions to 0644:

sudo chmod 0644 /etc/msmtprc

To test msmtp, you should receive an email from the address in the config file by entering this command:

echo -e 'Testing Mail\r\n\r\nHello from your computer!' | msmtp --debug --from=default -t purplemoose@gmail.com

Where purplemoose@gmail.com is your email address. If all goes well, you should see a bunch of text scroll by and an email appear in your inbox.

If all is not well, check the username, password, port and friends to ensure it's correct for your mail provider.

Making PHP sing

Well, speak emails at least. First, we need to edit php.ini (the PHP config file) to use msmtp instead of the default sendmail. To do this, open php.ini by typing:

sudo gedit /etc/php5/apache2/php.ini

Do note that the path might be different on some systems. Once you've got it open, find the line starting with

sendmail_path

and change it to

sendmail_path = '/usr/bin/msmtp -t'

This will now set PHP's default email program to msmtp. If you're not on Ubuntu, the path might be a bit different. You can find the path to msmtp by typing which msmtp.

Now all you need to do is restart Apache by typing

sudo service apache2 restart

and you'll be on your way to sending emails from your test server using PHP's normal mail() function.

Example PHP script

if(mail('purplemoose@gmail.com', 'Testing message from localhost', 'Hi there. Msmtp is working fine.'))
{
    echo "Message sent";
}
else
{
    echo "Could not send email";
}

This simply sends an email to purplemoose@gmail.com and prints an error if it fails, which it hopefully will not.