Using Gmail SMTP with Cloudflare Email Routing
Introduction
Cloudflare’s Email Routing feature lets you use your custom domain for email, forwarding messages to your Gmail inbox. If you want to send emails from Gmail as your custom domain (e.g., you@yourdomain.com), you may encounter authentication errors. This guide walks you through the correct setup to avoid the common 535 5.7.8 Username and Password not accepted error and ensure reliable sending and deliverability.
Cause of the Error
1
2
535 5.7.8 Username and Password not accepted.
For more information, go to https://support.google.com/mail/?p=BadCredentials
This error means Gmail’s SMTP server is rejecting your login — either due to incorrect credentials or security settings such as two-factor authentication (2FA) or restrictions on less secure apps.
This is especially common when using Gmail as an SMTP relay for addresses routed through Cloudflare Email Routing.
Why This Happens
- 2FA is enabled: If you have two-factor authentication on your Google account, you must use an App Password for SMTP, not your regular Google password.
- Incorrect credentials: The username or password may be wrong, or you may be using the wrong Google account.
- Security settings: Google may block access if you haven’t set up App Passwords or enabled the right settings.
- SMTP misconfiguration: Using the wrong server, port, or encryption can also trigger this error.
Solution: Sending Emails from Gmail with Cloudflare Email Routing
Follow these steps to resolve the issue and enable sending from your custom domain via Gmail:
1. Enable 2-Factor Authentication on Your Google Account
- Go to your Google Account → Security → 2-Step Verification and enable it.
2. Generate a Google App Password
- In your Google Account, go to Security → App Passwords.
- Generate and copy the 16-character App Password. This replaces your regular password for SMTP authentication.
3. Add Your Cloudflare Email as a Sending Alias in Gmail
- In Gmail, go to Settings → Accounts and Import → Send mail as → Add another email address.
- Enter your name and Cloudflare-routed email (e.g.,
you@yourdomain.com). - For SMTP server settings, use:
- SMTP Server:
smtp.gmail.com - Port:
587(TLS) or465(SSL) - Username: Your full Gmail address (e.g.,
yourname@gmail.com) - Password: The App Password you generated
- TLS/SSL: Enabled (preferably TLS on port 587).
- SMTP Server:
- Complete the confirmation process by clicking the link sent to your Cloudflare-routed email (which should appear in your Gmail inbox if routing is configured correctly).
4. (Recommended) Update SPF and DMARC Records in Cloudflare DNS
-
SPF Record: Add or update your TXT record
@to include both Cloudflare and Google:1
v=spf1 include:_spf.mx.cloudflare.net include:_spf.google.com ~all
-
DMARC Record: Add a DMARC record to improve deliverability and prevent spoofing:
1
v=DMARC1; p=none; rua=mailto:you@yourdomain.com
This reduces the chance of your emails being marked as spam.
Additional Troubleshooting
- Check credentials: Always use your Gmail address and the App Password, not your regular password.
- Security settings: If errors persist, check if “Allow less secure apps” is toggled (deprecated, not recommended).
- Network/firewall: Ensure your network isn’t blocking SMTP connections.
- DNS propagation: After updating DNS records, allow time for changes to propagate. Deliverability may be affected initially.
Summary (TL;DR)
| Step | Action |
|---|---|
| Enable 2FA | Google Account → Security → 2-Step Verification |
| Generate App Password | Google Account → Security → App Passwords |
| Add Alias in Gmail | Gmail Settings → Accounts and Import → Add another email address |
| SMTP Settings | smtp.gmail.com, Port 587, Username: Gmail address, Password: App Password, TLS enabled |
| SPF/DMARC (Cloudflare DNS) | Add/Update TXT records as shown above |
References
This guide is based on the following published resources below:
- How to send emails using Gmail SMTP with Cloudflare Email Routing:
- Google support on SMTP authentication errors:
- Cloudflare Community Forum - How to Use Gmail SMTP to Send from an Email Address which Uses Cloudflare Email Routing
- ImprovMX - Send Emails Using Gmail
Give some love to the author
Suggestions for improvement? Feel free to send me your feedback via email.
