Is your WordPress website hosted under Google Cloud Platform? If it is, well, good luck! You’re up for a challenge.
Configuring your outbound sending to GCP to be honest is quite challenging. Trying to make your WordPress Contact Form work is a bit tricky, but once you’ve manage to make it up and running, it is quite fulfilling. An extra points gained in setting up a feature under Google technology.
Google is very conscious when it comes to security, that is why all outbound connections are blocked. Yup! You read it right. As in blocked. So if you’re just going to rely in the built-in PHP Mailer of WordPress, it won’t work! Why did they do that you may ask? Well to avoid abuse which server-to-server SMTP are susceptible to. This way, spam emails and data injections will be prevented. The default SMTP port 25 is blocked on their end, which is why Google is suggesting to use their trusted third-party provider such as Mailgun, Mailjet, and SendGrid. But in this Motley Tech. Guide, we didn’t use any of those. Instead, we found two ways how to setup and send emails outbound without using the standard port 25. Firstly, a requirement check! You need to have the plugin below.
For WordPress websites hosted under Google Cloud Platform, you MUST have this plugin:
- WP Mail SMTP (by WPForms) – For email sending
Have it installed and activated, and we’re ready to setup everything. If you don’t have it yet, you can download it just by clicking “Add New” at the plugin page, then key-in and search for the plugin.
There are two ways to setup outbound sending with WP Mail SMTP securely:
- via Gmail SMTP (with the use of Gmail API)
- via Other SMTP (with the use of Transport Security Layer)
Via Gmail SMTP
Through Gmail SMTP, you need to have at least an email account setup under your company domain, e.g email@example.com. This is a requirement if you are about to setup an API credential and an OAuth consent screen. Because Gmail SMTP setup requires you to provide a Client ID and Client Secret, for WP Mail SMTP plugin and Gmail API to talk to each other.
Enough talk. Let’s do this.
- Assuming WP Mail SMTP is activated, click “Settings“.
- Once in the WP Mail SMTP page. Look at the “From Email” field. Make sure to provide an email address which emails are sent from.
- Check the box “Force From Email“.
- Look at the “From Name” field. Make sure to provide the name which emails are sent from.
- Check the box “Force From Name“.
- At the “Mailer” selection, choose “Gmail“.
- Check marked the box “Set the return-path to match the From Email“.
- Scroll down to the Gmail section, and look for “Authorized Redirect URI” field. Copy the value to a notepad.
- Now, let’s leave the WP Mail SMTP settings for a while. Make sure you’re logged in to your domain email account and then go to Gmail’s Application Registration.
- Once in the Gmail API Registration, leave the default option “Create Project” from the dropdown selection. Click the “Continue” button.
- You’ll be prompted to a next screen that says “API is enabled“. Click the “Go to Credentials” button to proceed.
- Now at the “Add credential to your project” screen, fill up the fields with the following values;
- In field “Which API are you using?“: Select “Gmail API“.
- In field “Where will you be calling the API from?“: Select “Web Server“.
- In field “What data will you be accessing?“: Select “User Data“.
- Then click the button “What credentials do I need?” to proceed.
- You’ll be prompted to a page where you need to setup your Open Authentication also known as OAuth. OAuth will provide permission for your website to use Gmail account to authenticate emails. Now provide the following values to the following fields:
- “Name“, value is anything you’d like. But we prefer the App Name you’ll be linking it with.
- Now go back to the notepad where we copied the Authorized Redirect value. Copy and paste it at the “Authorized Redirect Value” field.
- Click “Create Client ID” button.
- You will be prompted to a consent screen setup. All you have to do here is to provide a “Product Name” that will be prompted to users, and verify the email address that will be use. Click “Continue” button to proceed.
- Now you manage to create a Gmail web application. At the “Add credentials to your project” page, just click “I’ll do this later“. You will be redirected to “Credentials” page where list of App OAuth IDs can be found.
- Now select the newly created app, and select “Edit“.
- App details will show up. Copy the “Client ID” and “Client Secret” values to a notepad.
- Let’s go back to our WP Mail STMP plugin and go to Gmail section. Paste the “Client ID” and “Client Secret” values to its respective fields from the notepad.
- Click “Save Settings” button. After saving, the page will refresh.
- At the “Authorize” section, click “Allow plugin to send email using your Google account” button to authorize the plugin. This will open a login screen for Google.
- Provide the credentials and login, then you will be redirected to screen asking for permission to send email. Just click “Allow” button to proceed. You will be redirected again to WP Mail SMTP settings with a success message for linking the plugin.
- Now let’s check if the sending feature is working. Click “Email Test“.
- At the “Test Email” page, just type in any email address (preferably another personal address email you own) in the given field and hit the “Send Email” button. Should there be an error, there is an error notification that will be provided by the plugin pointing to the cause and what needs to be done. If you manage to successfully send and received an email, then CONGRATULATIONS!
Via Other SMTP
So we’ve tackled how to setup our outbound sending functionality via Gmail SMTP. A bit complicated but perfectly secured to use. This time we’ll use Other SMTP or our personal gmail account as SMTP to forward or send outbound emails. Is this approach secured? The answer is yes because it use TLS or Transport Layer Security that maximize data encryption and has its dedicated port which is port 587. This approach is pretty simple and straight forward. The example account that we will be using here is the free gmail account which obviously most of us have. So let’s dive into it shall we?
- Firstly, log in to your Gmail account.
- Click your account profile icon, and click “Google Account” button.
- New tab and page will be opened. At your Google Account page, click “Sign-in & Security“.
- At the “Sign-in & Security” page, scroll all the way down to “Apps with account access” section.
- Look for “Allow less secure apps” section, and switch it “On“. Close the page.
- Now let’s go to our Gmail settings by clicking the “cog” or “gear” icon, then select “Settings“.
- In your settings section, look and select “Forwarding and POP/IMAP“.
- Select the following options: At the “POP Download” section, select “Enable POP for all mail“. At the “IMAP Access” section, select “Enable IMAP“.
- Scroll down a little, and click “Save Changes” button. You’re done at the Gmail side.
- Now let’s go and access our WordPress admin. account. Hover to “plugins“, and then select “Installed Plugins“.
- Look for “WP Mail SMTP” plugin. We assume the this plugin is already activated so just click “Settings“.
- At the WP Mail SMTP settings, fill up the following fields:
- provide email for “From Mail“,
- check “Force From Mail“,
- provide name for “From Name“,
- check “Force From Name“.
- Lastly check “Set the return-path to match From Email“.
- At the “Mailer” section, select “Other SMTP“.
- Upon selecting “Other SMTP”, a configuration form below will show up. Provide the following values for the following fields:
- “SMTP Host” field, value is “smtp.gmail.com“.
- “Encryption” field, select “TLS“.
- “SMTP Port” field, value is “587“.
- “Authentication“, turn it “on“.
- Upon turning on the “Authentication”, a SMTP credential fields will show below. Provide the following values to the following fields:
- “SMTP Username“, value is your gmail email address.
- “SMTP Password“, value is your password to your gmail account.
- Once done, click the “Save Settings” button.
- Now to test if your configuration is working properly, scroll to the top most part of WP Mail SMTP plugin and click “Email Test“. Just type in any email address (preferably another personal address email you own) in the given field and hit the “Send Email” button. Should there be an error, there is an error notification that will be provided by the plugin pointing to the cause and what needs to be done. If you manage to successfully send and received an email, then CONGRATULATIONS!
So we’ve have managed to successfully setup our sending feature of our WordPress website hosted under Google Cloud Platform in two says which both are secured. One approach use Gmail API, and the other approach use personal gmail account that maximize TLS (Transport Layer Security) for email encryption. Both are a bit tedious at first but once you get use to it, it will be easy peasy lemon squeezy.