Instant new mail
Optional. By default your server checks for new mail every minute. With this, Gmail notifies it within seconds instead, through Google Cloud Pub/Sub.
Pub/Sub needs billing turned on in your Google Cloud project, but one mailbox's notifications fit comfortably in its free tier. Below, <your-server> means your server's address without https://, such as echelon-inbox.your-subdomain.workers.dev.
1. Turn on billing
- Open Billing with your project selected.
- Click Link a billing account and choose one, or create one with a card.
2. Create the topic
- Enable the Cloud Pub/Sub API.
- In Pub/Sub → Topics, click Create topic. Topic ID:
gmail-push. Untick Add a default subscription, then click Create. - Open the topic's Permissions (or Show info panel) and click Add principal. Enter
[email protected], choose the role Pub/Sub Publisher, and save. This lets Gmail post to your topic.
If your project is in a Google Workspace organization and Google refuses that principal with a domain-restriction error, an organization policy is blocking it. A Workspace admin can allow it under IAM & Admin → Organization policies → Domain restricted sharing for this project.
3. Create the signing account
Pub/Sub signs each notification so your server knows it's genuine.
- In IAM & Admin → Service accounts, click Create service account.
- Service account ID:
gmail-push-auth. Click Done, skipping the optional steps.
4. Create the subscription
In Pub/Sub → Subscriptions, click Create subscription:
- Subscription ID:
gmail-push-worker - Topic:
gmail-push - Delivery type: Push. Endpoint URL:
https://<your-server>/push/gmail - Tick Enable authentication and choose the
gmail-push-authservice account. Leave Audience empty. - Expiration period: Never expire. Retry policy: Retry after exponential backoff delay.
Click Create.
5. Tell your server
In the Cloudflare dashboard, open Workers & Pages → your server → Settings → Variables and Secrets and add two secrets. Replace your-project-id with your Google Cloud project ID, shown on the project picker:
| Name | Value |
|---|---|
GMAIL_TOPIC | projects/your-project-id/topics/gmail-push |
PUSH_SERVICE_ACCOUNT | [email protected] |
Within a minute, your server subscribes each mailbox to Gmail notifications, and its status page shows New mail: Instant. Send yourself an email to check: it should be tagged within seconds.
If Pub/Sub's requests fail with an authentication error in an older Google Cloud project (created before April 2021), grant service-<project-number>@gcp-sa-pubsub.iam.gserviceaccount.com the Service Account Token Creator role on gmail-push-auth. If anything is misconfigured, your server falls back to checking every minute.
Prefer Terraform?
The repository's infra/gcp folder sets up all of the above in one terraform apply. See Deploy from the command line.