Google Gmail OAuth2 Setup Guide for Render & Cloud Deployment
This step-by-step guide explains how to set up Google Gmail OAuth2 authenticationfor sending automated emails and OTP verification codes from cloud hosting environments like Render.com.
Why Use Gmail OAuth2 on Render?
- **Bypasses Port Blocking:**Uses standard HTTPS API tokens over port
443/587, which cloud hosting providers (Render, AWS, DigitalOcean) never block. - **No Password Lockouts:**Eliminates "Less Secure Apps" errors or random password authentication blocks by Google Security.
- **High Deliverability:**Direct Google token authentication ensures fast (0.5s) email delivery to recipient inboxes.
Prerequisites
- A Gmail or Google Workspace account (
your-email@gmail.com). - Access to Google Cloud Console.
- Access to Google OAuth2 Playground.
Step-by-Step Setup Instructions
Step 1: Create a Google Cloud Project
- Open the Google Cloud Console.
- Click the project dropdown in the top bar and click New Project.
- Set Project Name:
Retail POS Email Service-> Click Create.
Step 2: Enable the Gmail API
- In the top search bar of Google Cloud Console, search for Gmail API.
- Click Gmail APIfrom the Marketplace / API list.
- Click Enable.
Step 3: Configure the OAuth Consent Screen
- In the left navigation menu, go to APIs & Services→ OAuth consent screen.
- Select Externalas the User Type -> Click Create.
- Fill in the required fields:
- App Name:
Retail POS System - **User Support Email:**Your Gmail address
- **Developer Contact Information:**Your Email address
- App Name:
- Click Save and Continue.
- **Scopes:**Click Add or Remove Scopes:
- Search for
https://mail.google.com/(Gmail API full access). - Check the box next to
https://mail.google.com/-> Click Update.
- Search for
- Click Save and Continue.
- **Test Users:**Click + Add Users:
- Add your sender Gmail address (
your-email@gmail.com).
- Add your sender Gmail address (
- Click Save and Continue.
Step 4: Create OAuth2 Credentials (Client ID & Client Secret)
- In the left navigation menu, go to Credentials.
- Click + Create Credentials→ OAuth client ID.
- Fill in the details:
- Application type:
Web application - Name:
Render Backend Client - **Authorized redirect URIs:**Add exactly:
https://developers.google.com/oauthplayground
- Application type:
- Click Create.
- Copy and save your Client IDand Client Secret:
GMAIL_CLIENT_ID(e.g.,123456789-xyz.apps.googleusercontent.com)GMAIL_CLIENT_SECRET(e.g.,GOCSPX-your_client_secret)
Step 5: Generate the Refresh Token via OAuth2 Playground
- Open the Google OAuth 2.0 Playground.
- Click the Gear Iconin the top-right corner.
- Check the box: "Use your own OAuth credentials".
- Paste your credentials:
- **OAuth Client ID:**Paste
GMAIL_CLIENT_ID - **OAuth Client Secret:**Paste
GMAIL_CLIENT_SECRET
- **OAuth Client ID:**Paste
- On the left sidebar under Select & authorize APIs:
- Scroll down to Gmail API v1.
- Expand it and check
https://mail.google.com/.
- Click Authorize APIs.
- Log into your Gmail account and click Continue / Allow.
- You will be redirected back to OAuth Playground. Click Exchange authorization code for tokens.
- Copy your Refresh Token (
GMAIL_REFRESH_TOKEN).
Step 6: Add Variables to Render Environment
In your Render Dashboard (or .env file), set the following key-value pairs:
EMAIL_PROVIDER=GMAIL
EMAIL_USER=your-email@gmail.com
GMAIL_CLIENT_ID=123456789-xyz.apps.googleusercontent.com
GMAIL_CLIENT_SECRET=GOCSPX-your_client_secret_here
GMAIL_REFRESH_TOKEN=1//04_your_refresh_token_here
Verification & Testing
Once deployed on Render with these environment variables, check your container logs:
[EMAIL DEBUG] Target: customer@example.com | Provider Mode: GMAIL | User: YES (your-email@gmail.com)
[EMAIL MODE] Sending strictly via Gmail OAuth2 REST API (Port 443) to customer@example.com...
[GMAIL API] Fetching OAuth2 access token for your-email@gmail.com...
[GMAIL API SUCCESS] Sent to customer@example.com: {"id": "191...", "threadId": "191..."}
[!TIP] **Why it works so fast (0.2s):**The backend uses Google's official Gmail REST API over Port 443 (HTTPS). It does notrely on raw SMTP socket connections, ensuring instant email delivery without any port blocking or socket timeouts on Render.