# How to Automate Gmail Templated Replies & Follow-ups using the API

- Tool: API Automations
- Difficulty: Intermediate
- Fix time: 30-60 minutes
- Compatibility: OAuth 2.0 required | gmail.send scope (Sensitive, governed by Restricted policy) | Google Cloud project required
- Last updated: May 2026

## TL;DR

Automate Gmail templated replies and follow-up sequences using the Gmail API's users.messages.send endpoint. Messages must be RFC 2822-formatted and base64url-encoded (not standard base64). Threading requires matching In-Reply-To and References headers plus the original threadId. Hard sending cap: 500 messages/day for @gmail.com, 2,000/day for Google Workspace accounts.

## Best practices

- Always use base64url encoding for the 'raw' field — test by decoding your encoded string before the first production send
- Include proper threading headers (In-Reply-To, References) for all follow-up messages — this is what keeps replies in the same thread
- Build a suppression list: before each send, check if the recipient has replied or unsubscribed, and skip them
- Cap your automation at 80% of the daily limit to leave headroom for manual emails sent from the same account
- Store draft IDs in a database rather than memory — your follow-up scheduler needs to survive restarts
- Add the List-Unsubscribe header to comply with CAN-SPAM: List-Unsubscribe: <mailto:unsub@yourdomain.com>
- Test RFC 2822 messages with a single recipient before bulk runs — encoding errors don't show up until the API call

## Frequently asked questions

### Why do my follow-up emails start new threads instead of replying to the original?

You need three things for proper threading: the In-Reply-To header set to the original message's Message-ID value, the References header containing the chain of all previous Message-IDs, and the threadId field in the API request body. Missing any one of them causes Gmail to create a new thread. Fetch the original message with format=metadata to get its Message-ID header.

### What is the Gmail API daily sending limit?

Personal @gmail.com accounts: 500 messages per day, max 500 recipients per email. Google Workspace accounts: 2,000 messages per day per user, max 100 recipients per message via API (not 500 for API calls). Exceeding these limits returns a 429 error. The limits reset at midnight in the account's timezone. These are hard infrastructure limits that cannot be raised.

### Why does Gmail reject my message with a 400 error about base64?

The Gmail API requires base64url encoding, not standard base64. The difference: base64url replaces '+' with '-' and '/' with '_'. In Python, use base64.urlsafe_b64encode(). In JavaScript, after standard btoa(), replace all + with - and / with _. Standard base64 encoding of ASCII-only content happens to work sometimes because those characters don't appear, but non-ASCII content will fail.

### Can I send from a different email address (alias) than the authenticated account?

Only if the alias is configured in Gmail Settings → Accounts → Send mail as. The From header must match either the primary account or a verified send-as alias. Using an unverified From address results in a 400 error. You can fetch the list of valid send-as addresses with users.settings.sendAs.list.

### What happens when I hit the rate limit on messages.send?

You'll receive a 429 or 403 error. If it's the daily sending cap (500/day for Gmail, 2,000/day for Workspace), stop sending for the day — the limit resets at midnight. If it's the per-minute quota (15,000 units/min, with send costing 100 units = max 150 sends/minute), implement exponential backoff starting at 1 second, doubling each attempt, max 64 seconds.

### Is the Gmail API free for sending email?

Yes, the Gmail API is free with no per-message charges. You pay only for any Google Cloud infrastructure (like Pub/Sub if you use push notifications). The sending limits (500 or 2,000/day) are the practical constraint, not cost.

### Can RapidDev help build a custom Gmail email automation system?

Yes. RapidDev has built 600+ apps including sales automation and support systems using the Gmail API. We handle OAuth setup, RFC 2822 message construction, threading, rate limit management, and building the dashboard to manage sequences. Contact us for a free consultation at rapidevelopers.com.

---

Source: https://www.rapidevelopers.com/api-automations/how-to-automate-gmail-templated-replies-and-follow-ups-using-the-api
© RapidDev — https://www.rapidevelopers.com/api-automations/how-to-automate-gmail-templated-replies-and-follow-ups-using-the-api
