As mentioned in my previous post, I use msmtp on my server for sending email from my blog to notify of comments etc. Here I’m going to write how I configured it for use with Gmail for sending emails. Note that I’m actually using a Google Apps for Domains account here, not a standard Gmail account.
When I switched over to my new VPS, I had some issue in setting it up, when, in reality, setting msmtp up to use Gmail as the actual mail sending couldn’t be much simpler. Turns out most of my issues where caused by not installing a single package, which I believe is actually a recommended package when pulling msmtp from apt.
First off, install the packages we need to get up and running:
sudo apt-get install mstmp ca-certificates msmtp-mt
Once these are installed, we need to write a default config; by default, msmtp looks at
/etc/msmtprc, so that’s the file I created:
# Set defaults.
# Enable or disable TLS/SSL encryption.
# Setup WP account's settings.
user $_ email@example.com _$
password $_ password _$
from $_ firstname.lastname@example.org _$
You’ll need to change the relevant parts (those wrapped in $_ _$, as well as the account name) to suit whatever your settings actually are. I should note that the logfile location can of course be changed too.
Save that, and then we’ll create the directory for the log file, and change the permissions for the log directory and the config file, as msmtp won’t run if the config file has permissions that aren’t restrictive enough.
sudo mkdir /var/log/msmtp
sudo chown -R www-data:adm /var/log/msmtp
sudo chown 0600 /etc/msmtprc
Now that’s configured, I chose to setup logrotate for that directory, though this step is optional, it should help keep things a little tidier in
/var/log; create the file
/etc/logrotate.d/msmtp to look something like this:
You may choose to not set this up, or set it up differently, depending how much you’re going to use msmtp, but for infrequent use, that should be fine.
Now we just need to edit the php config, and then test it, which is simple enough. Edit
/etc/php5/apache2/php.ini and find the sendmail path, changing it from the default:
sendmail_path = "/usr/bin/msmtp -C /etc/msmtprc -a account_name -t"
Make sure to change the account name to whatever you specified in the config file!
Finally, restart apache by running
sudo service apache2 restart, and then we can test it by entering
php -a, and then using the following at the php prompt:
mail ('email@example.com', 'Test Subject', 'The body text');
This will show any errors that occur after entering the first line, but hopefully it’ll have sent successfully and you’ll receive the ‘Test Subject’ email! That’s all, your php sites should be able to use Gmail’s SMTP server to send mail out using your Ubuntu server.
Now, I’m no expert on setting up a completely secure Linux server, but that’s how I got msmtp setup and working for me; if there’s something horribly wrong with this, please let me know in the comments, or however you feel the need to get in touch, and I’ll gladly update the post.