Share this: Twitter

Reddit

Email

LinkedIn

Facebook



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 [email protected] user [email protected] 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("[email protected]","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.

0

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

Share this: Twitter

Reddit

Email

LinkedIn

Facebook



Like this: Like Loading...