Authorizing WordPress Plugin to use Gmail API on Google Cloud Platform

software: WordPress
cloud: Google Console
plugin: WP Mail SMTP

The Problem

Google is constantly changing the layout of their backend and also modifying how users authorize apps to use their APIs. It seems like it’s ever increasingly complicated, but I digress.

With any website, it is nice to have the capacity to send mail. This is useful for contact forms, but also for error messages and automated scripts. One can use the built in PHPmail() function, but connecting with proper SMTP mailer is ideal, in order to have proper security and avoid getting emails bounced.

WordPress now has many plugins that allow you to use the Gmail API to send mail from your website. Problem is that Google, at the time of writing this, has pretty strict authentication standards: OAuth 2.0 protocol.

For this solution, we’ll be using the WP Mail SMTP plugin, but there are others that authenticate in the same way.

The Solution

First, install WP Mail SMTP from your ‘Add New Plugin’ section of the dashboard.

Add a new plugin in WordPress

Quick search for ‘gmail’ will give a list of all the plugins to install, I like to use WP Mail SMTP.

Once activated, you can browse to the Settings page of the plugin and start entering in the details.

Select Gmail Mailer to begin setting up Google API

Fairly simple. Much like any other plugin install. Except now, you’ve got to enter the sphere of Google Cloud Platform, a confusing, convoluted world of programmers.

WP Mail SMTP has a configuration step by step guide that used to work, except now it’s not up to date with how to add credentials.

Google Cloud Platform is where you want to go now. Here you’ll give WP Mail SMTP access to use your Gmail account.

First, you’ll need to create a project. Name this project something meaningful, you might have to reference it again.

Once it’s created, make sure you’ve got it selected from the drop down menu at the top of the screen, and navigate to the APIs and Services tab and select Credentials.

From here, select Create Credentials from the top and select OAuth.

Create API Authentication Credentials to use in WordPress Mailer

You’ll be directed to the consent screen for OAuth, this will be a web application.

Create OAuth Client ID in Google Cloud

Here you only need to add the redirect URI given to you by your plugin, in this case it will be

https://connect.wpmailsmtp.com/google/

from WP Mail SMTP settings page on your WordPress site. Click create and you’re almost there.

Head into the OAuth Consent Screen tab, where you want to move this now into published mode.

Publish the API that now has OAuth authentication

After this, you need to now give the Gmail API authority to make emails on your behalf. To do this, you have to navigate to the Library within your APIs & Services screen and search for Gmail API.

Select the Gmail API and enable the service.

To finally get the Client ID and Client Secret, click on the Credentials tab and you will see under the OAuth 2.0 Client IDs will be the ID you just created. On the right hand side will be a little button to edit the key, if you click that, you will be able to find your Client ID and Client Secret that you will have to copy/paste into the WordPress Plugin WP Mail SMTP settings.

Copy and Paste Client ID and Client Secret into WP Mail SMTP settings within WordPress

Once you’ve copy/pasted the settings into the WordPress WP Mail SMTP settings page, click save settings and your page will refresh and this section will now have a big orange button asking you to allow the plugin to send emails using your Google account.

After clicking that big orange button, you will be asked to authenticate yourself using your Google account, make sure to select the right Google account to use for this, which is the account you used to setup the whole API Key above from the start.

Once you have successfully linked WP Mail SMTP with Google API, you are now ready to send a test email to see if it’s actually working.

Conclusions

This is becoming quite tedious to setup Gmail to use as your website emailer. It would seem more logical to take a relaxing walk around the block than to go through with this again!

However, remember that Google does this all to make sure that the emails you’re sending are actually from you and not hijacked to create spam. And also remember that you can only send so many emails using the Gmail API in a day, so be wary about email lists that a gluttonous.

It is also important to have your SPF, DKIM, and DMARC all configured if possible. This is done by setting the adding DNS records that you get from your Gmail admin console. The helpful links are beneficial to follow.

Troubleshooting

You may get an error message when you test with an email:

"error": {
    "code": 403,
    "message": "Gmail API has not been used in project XXX before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/gmail.googleapis.com/overview?project=XXX then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",

If you do get this, follow the link it provides you to solve all your problems.

Leave a Reply

1 × five =