Contact Form 7 Cannot Send Emails from DigitalOcean Server

By | April 3, 2016

“Failed to send your message. Please try later or contact the administrator by another method.”

Are you seeing the above error when trying to send a message via the Contact Form 7 plugin for WordPress?

The Contact Form 7 plugin sends email using a mail server installed on your DigitalOcean server. If no mail server is installed, then your visitors will see the above error whenever they try to submit a message via the form.

This post will walk you through the steps required to setup the lightweight email server msmtp on your Digital Ocean server, so that WordPress plugins like Contact Form 7 can send email messages.

INSTALL MSMTP

SSH into your server.

First, use the package manager to check if msmtp is already installed:

dpkg -l msmtp

If it’s not, then issue the following command to install it:

sudo apt-get install msmtp

CONFIGURE MSMTP

Use the version option to find out where msmtp’s configuration file is located:

msmtp --version
Platform: x86_64-pc-linux-gnu
TLS/SSL library: GnuTLS
Authentication library: GNU SASL
Supported authentication methods:
plain scram-sha-1 cram-md5 gssapi external digest-md5 login ntlm
IDN support: enabled
NLS: enabled, LOCALEDIR is /usr/share/locale
Keyring support: none
System configuration file name: /etc/msmtprc

Display the contents of the configuration file:

more /etc/msmtprc

If a configuration file doesn’t exist, create one using the vi program:

sudo vi /etc/msmtprc

Replace the contents of the configuration file with the text below, substituting “username@gmail.com” and “yourpassword” with your own Gmail address and password:

# Gmail
account  gmail 
host   smtp.gmail.com 
port   587 
from   username@gmail.com
user   username@gmail.com
password  yourpassword
auth   on 
tls   on 
tls_trust_file /etc/ssl/certs/ca-certificates.crt 
 
# Default account to use
account default : gmail

CONFIGURE PHP

You need to tell PHP, via its configuration file, that you want to use msmtp as your mail server.

First, make a backup copy of the php.ini file before making any changes to it:

cd /etc/php5/apache2
cp php.ini php_BAK.ini

Edit php.ini using vi:

sudo vi php.ini

Find the “sendmail_path” parameter. You can easily find it by pressing the forward slash key ‘/’, typing “sendmail_path” and hitting the enter key.

;sendmail_path =

Uncomment the line by deleting the semicolon and add the following:

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

Save the file. In vi, you can save the file by pressing the ‘ESC’ key, typing “wq” and hitting enter.

YOU’RE DONE

You should now be able to submit emails via the Contact 7 Form. If you’re still getting error messages, follow the troubleshooting section below.

How did it go? Are you still having problems? Leave a comment below!

TROUBLESHOOTING

The Apache error log can offer insight into what’s causing the problem:

cd /var/log/apache2
tail error.log

Look for any msmtp error messages in the output.

AUTHENTICATION FAILURE

If you have dual factor authentication enabled for your Gmail account, you will likely see this error in the Apache log:

msmtp: authentication failed (method PLAIN)
msmtp: server message: 534-5.7.9 Application-specific password required. Learn more at
msmtp: server message: 534 5.7.9  https://support.google.com/accounts/answer/185833 d187sm10698474qhc.38 - gsmtp

To resolve this, you must create an Gmail app password for the msmtp application, which basically involves logging into your gmail account and creating a password for this specific application.

Follow the link in the error message for more detailed information. Here is an outline of the steps to follow:

  1. Follow this link to access the Gmail app passwords page
  2. Sign in with your Gmail ID and password
  3. Click the “Select app” dropdown box
  4. Select “Other (custom name)”
  5. Type “msmtp” (or any name that you prefer) into the text box
  6. Click the “Generate” button
  7. Copy and paste the generated app password into the msmtp configuration file, replacing your gmail password
  8. Save the configuration file
  9. Try sending another message using the Contact 7 Form

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*