Use Gmail SMTP Servers for Sending Emails from WordPress
After quite a bit of back and forth between sendmail, postfix, and exim, I’ve settled on using msmtp for sending emails from my servers/droplets at DigitalOcean (affiliate link).
MSMTP is very lightweight and has the ability to send emails via an existing SMTP server, like Gmail’s or Yahoo’s.
To get it working, there’s a few tricks. I’ve pieced this together from this post and this post. And when on DigitalOcean, there’s an IPv6 issue that causes major delays in sending the email, which there’s a fix for at the end of this post.
1. Install msmtp
sudo apt-get install msmtp
2. Configure msmtp to use Gmail
Open up /etc/msmtprc
as root: sudo nano /etc/msmtprc, and add the following, removing whatever else is there:
# Gmail/Google Apps account gmail host smtp.gmail.com port 587 from example@gmail.com user example@gmail.com password enter-password-here! auth on tls on tls_trust_file /etc/ssl/certs/ca-certificates.crt # Default account to use account default : gmail
You’ll want to replace the user
directive with a valid Gmail email address, a Gmail account or a Google Apps email address will work, too. Don’t forget to change enter-password-here!
to the actual password for the Gmail account your using.
Save /etc/msmtprc
.
3. Remove Sendmail
Run this:
sudo apt-get remove sendmail-bin
4. Setup Some Aliases
Lots of software on Linux systems uses the sendmail command. Instead, we’re using msmtp, so we’re essentially invoking msmtp when the sendmail
command is run.
sudo ln -s /usr/bin/msmtp /usr/sbin/sendmail sudo ln -s /usr/bin/msmtp /usr/bin/sendmail sudo ln -s /usr/bin/msmtp /usr/lib/sendmail
5. Tell PHP About msmtp
First, locate your php.ini
file that’s being used by Apache. It’s typically in /etc/php5/apache2/php.ini
. If that’s not it, use PHP’s phpinfo()
function to find the location of your php.ini
file.
Find sendmail_path =
in php.ini
and replace it with this:
sendmail_path = "/usr/bin/msmtp -t"
Now you should be able to send mail using PHP’s mail() function, which will use the Gmail SMTP server to send emails. Add this to a PHP file and access it through your browser to see if it works:
<?php if(mail("receipient@domain.com","A Subject Here","Hi there,nThis email was sent using PHP's mail function.")) print "Email successfully sent"; else print "An error occured"; ?>
6. Disable IPv6 If You Experience Slowness
Open up /etc/gai.conf
like so:
sudo nano /etc/gai.conf
Now, look for a line that looks like this: #precedence ::ffff:0:0/96 100
. Uncomment that line (remove the #) and save /etc/gai.conf
. An explanation of why this helps can be found in this comment at the DigitalOcean article.
All Done
That should be it. If you run into any issues please do leave a comment, I’ll do my best to help you out. I may have missed a part, so no guarantees this will work for you. It does however work wonderfully on a DigitalOcean droplet that’s running Ubuntu 14.04 with a pretty standard LAMP stack.
You should now be able to send email from WordPress on DigitalOcean.
Well, now what?
Work with Me
I'm available for hire and always taking new clients, big and small. Got a project or an idea you'd like to discuss? Startup plan but no developer to make it happen? Just get in touch, I'd love to see if I can help you out!
Leave some Feedback
Got a question or some updated information releavant to this post? Please, leave a comment! The comments are a great way to get help, I read them all and reply to nearly every comment. Let's talk. 😀
Longren.io is proudly hosted by DigitalOcean

Hello,
Thank you for a nice and easy tutorial. It works just
fine. However, the emails are sent from WordPress (in details I can see
the correct email however in from it says WordPress just as email sent
from you would say Tyler). How could I have it send email from John Doe?
Thank you,
Goran
Are the emails coming from a contact form or something? You may need to configure whatever is sending the emails to send as a certain name and email address.
Let me know, I can help more if needed.
Hi Tyler,
I already figured it out. I had to configure as you said whatever was sending emails.
Great tutorial. Thanks!
Great post! Thanks.
Should the “tls_trust_file” be the location listed above, or the location of your domain ssl cert? Mine is not located “/etc/ssl/certs/ca-certificates.crt”, but there is a cert there….
The location above is correct, it should be pointed to:
/etc/ssl/certs/ca-certificates.crt
Is there any way to specify the send email as in Gmail I have added other emails like myemail@mydomain.com ?
Thanks! This guide is exactly what I was looking for.
I did run into a couple of issues. First, for some reason there was no configuration file after I installed msmtp, so I ended up creating one myself. Also, if you use dual factor authentication with your Gmail account, then you’ll need to generate an app password that msmtp can use. It helps to check the apache log file for errors.
Thanks for sharing..
Thanks for the share. I am experiencing some issues following the exact steps on a droplet with OpenLitespeed installed. Could you please create a tutorial for the same?