Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save 6220119/4ed99bc428d8f7fc8faeda9c05e6f4c6 to your computer and use it in GitHub Desktop.
Save 6220119/4ed99bc428d8f7fc8faeda9c05e6f4c6 to your computer and use it in GitHub Desktop.
Using Gmail SMTP with Cloudflare Email Routing: A Step-by-Step Guide

Using Gmail SMTP with Cloudflare Email Routing: Step-by-Step Guide

Learn how to send emails through Gmail SMTP with Cloudflare Email Routing in this comprehensive guide.

Step 1: Enable 2-Factor Authentication

To proceed with this method, ensure that you have enabled two-factor authentication for your Google account. If you haven't done so already, you can follow the link to set it up → Enable 2FA in your Google account.

Step 2: Create an App Password for Mail

In your Google account settings, create an App Password specifically for Mail. Follow this link to create the App Password → Create an App Password (You will need to copy and use this password later along with your Gmail address in the Google SMTP server settings in the "Add another email address" form).

When creating the App Password, select "Mail" as the app and choose your computer as the device. Click on "Generate" and make sure to copy the generated password. You will need it later in the process.

Step 3: Add Your Cloudflare-Routed Email Address to Gmail

Open Gmail and navigate to Settings → Accounts → Send mail as. In this section, click on "Add another email address" and fill out the form with your name and your Cloudflare-routed email address. Untick the "Treat as an alias" option and click on "Next Step."

Step 4: Fill Out the Next Form

SMTP Server: smtp.gmail.com
Port: 587
Username: Your Gmail address (including @gmail.com)
Password: The App Password you generated in Step 2
Leave TLS enabled
Click on Add Account

You will receive an email from Gmail asking you to confirm ownership by providing a code. Enter the code in the dialog box or click the link provided in the confirmation email to complete the process.

Step 5: Setup SPF Records & DMARC Policy in Cloudflare DNS

SPF Record

  • Type: TXT
  • Name: @
  • TTL: auto
  • Content:
v=spf1 include:_spf.mx.cloudflare.net include:_spf.google.com ~all

DMARC Policy

If you wish to set up a DMARC Policy or already have one, ensure that the policy's p parameter is set to none. Otherwise, your outbound emails may fail to authenticate and get blocked.

You can use Cloudflare's DMARC Management to set up a policy for monitoring outbound emails.

Example TXT Record:

v=DMARC1; p=none; rua=mailto:<your-email-to-report>

That's it!

You have successfully configured Gmail SMTP with Cloudflare Email Routing. Now, when composing a new message in Gmail, you can select your new email address from the list. Additionally, when replying to an email received at your new address, the new address should automatically populate in the From: field.

Credits

This guide is based on the following published resources below.

@pH-7
Copy link

pH-7 commented Aug 19, 2024

Btw, for anyone who need to apply more than one email to this policy, what you will need to do is just adding a , comma, so it would look like this after adding a second email address to it

v=DMARC1; p=none; rua=mailto:[email protected],[email protected]

Hope it helps 🙂

@FaBRiK74
Copy link

I try to move from my old Google workspace (let's say used mydomain.com domain). Followed the steps above but I have a strange issue The domain and the email is now handled by Cloudflare. I have set up the email MX records on Cloudflare. The situation: my original Google workspace email address is e.g. [email protected], the alternate email address which I'd like to use is [email protected]
I set up mydomain.com in Cloudflare also set up the MX records. I also set up the routing, so when I send an email to [email protected] it arrives to [email protected]
When I send an email from [email protected] but using [email protected] (with the settings above), everything is OK, it sends the email.
After I suspend the user in Google admin, I still can receive the emails sent to [email protected] on [email protected], but when I want to send out email from [email protected] in the name of [email protected], it gives back error: You no longer have access to [email protected]. To send the email, please choose a different sender address and try again. More information: https://support.google.com/mail/answer/22370

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment