Building Reliable Email Systems
Using Pub/Sub Messaging

In today’s digital landscape, efficient communication between software systems is crucial. This post explores how Pub/Sub messaging enables scalable, decoupled, and fault-tolerant email systems.

Jul 25, 2025

Moltech Solutions Inc.

90% Improved Reliability

Pub/Sub ensures emails are delivered even during spikes or partial failures.

80% Better Scalability

Decoupling services allows for independent scaling of producers and consumers.

65% Faster Throughput

Message queues prevent bottlenecks and improve async processing speeds.

Building Reliable Email Systems Using Pub/Sub Messaging

“In today’s digital landscape, efficient communication between software systems is crucial for business success.”

This opening sentence establishes the fundamental business problem being addressed. Modern businesses rely on multiple software systems that need to exchange information efficiently. For example:

  • CRM systems need to know about new customer inquiries
  • Support ticket systems need to track email conversations
  • Marketing automation tools need to monitor customer responses
  • Workflow systems need to initiate processes based on incoming communications

When these systems can’t communicate efficiently, businesses experience delays, lost information, and missed opportunities. The statement frames the technical discussion that follows in terms of business value.

In this post, we’ll explore how Pub/Sub works for email integration, step-by-step implementation guidance, and best practices to optimize deliverability and performance.

1. What Is Pub/Sub?

Publish-subscribe is a messaging pattern where senders (publishers) categorize messages into classes without knowledge of which receivers (subscribers) will receive them. Similarly, subscribers express interest in specific message classes and only receive messages that match their interests, without knowledge of which publishers are generating the messages.

This decoupling offers several advantages:

  • Scalability: Systems can handle large volumes of messages without becoming bottlenecks
  • Flexibility: New publishers and subscribers can be added without disrupting existing components
  • Resilience: Failure in one component doesn’t necessarily affect others
Pubsub Preview
  1. Publisher sends an “EmailEvent” with payload (recipient, template ID, data).
  2. Pub/Sub Service persists and routes the event.
  3. Email Subscriber picks up events and calls your SMTP or email API provider (e.g., SendGrid).
  4. Analytics Subscriber logs events for metrics and monitoring.

The Email Integration Challenge

Email remains a critical communication channel for businesses, but integrating email functionality with modern applications presents several challenges:

  • Managing high volumes of incoming and outgoing messages
  • Ensuring reliable delivery despite network issues
  • Processing emails asynchronously without blocking user interactions
  • Tracking email statuses (delivered, opened, bounced)
  • Implementing complex workflows triggered by email events

Pub/Sub architectures address these challenges elegantly by providing a robust foundation for email processing pipelines.

Implementing Pub/Sub for Email Integration

Core Components

A typical pub/sub-based email integration system consists of:

  1. Publishers: Components that generate email-related events (user registrations, order confirmations, marketing campaigns)
  2. Message Broker: The central hub that manages message routing (Kafka, RabbitMQ, Google Cloud Pub/Sub, AWS SNS/SQS)
  3. Subscribers: Services that consume email events and perform specific actions (sending emails, updating databases, triggering workflows)
  4. Email Service Provider (ESP): The external service responsible for actual email delivery (SendGrid, Mailchimp, Amazon SES)

Gmail Integration Using Pub/Sub

Gmailfs API with pub/sub notifications provides a powerful way to integrate email functionality into your applications. Lets explore how this works in practice:

1. Google Cloud Setup
  • Open the Google Cloud Console.
  • Click on IAM and Admin
  • Create a project and name it (e.g., “Your Project Name”)
Gmail Integration Using Pub/Sub
2. Enable Required APIs

Go to APIs & Services > Library and enable the following:

  • Gmail API
  • Cloud Pub/Sub API
Enable Required APIs
Cloud Pub/Sub API
3. Create Service Account and Credentials:
  • Go to IAM & Admin > Service Accounts > Create Service Account
Create Service Account and Credentials
My Gmail Listener
  • Grant access to this service account with the role: Pub/Sub Publisher.
  • Go to the Keys tab and create a new key.
My Gmail Listener
  • Select Key type, we choose JSON
My Gmail Listener Private Key
  • Download the JSON key file (keep it safe!). Please note, if this is lost, it can create a security risk.
4. We need to Grant Gmail Access via OAuth2

Please note: Google does not allow service accounts to access Gmail unless you are a G Suite/Workspace admin using domain-wide delegation. For personal accounts, we need to use OAuth access.

Grant Gmail Access via OAuth2
  • Open APIs & Services > Credentials and click on Create Credentials
  • Choose OAuth client ID
  • Application Type, choose based on your application. (We use Web application in this example.)
Create OAuth Client ID
  • Either you can add JavaScript Origins or Redirect URIs for your application. We will use the Redirect URI as we need to configure a webhook as below:
    https://yourdomain.com/oauth2callback (adjust as needed)
  • Please save the Client ID and Client Secret.

Now open the Cloud Shell.

Cloud Shell
5. Create a Pub/Sub Topic in Google Cloud

First, you’ll need to create a topic in Google Cloud Pub/Sub that will receive notifications from Gmail. Run the following command:

1 gcloud pubsub topics create gmail-notifications

Please note: gmail-notifications is the topic name — you can choose your own name.

Now, you need to subscribe to the topic.

1 gcloud pubsub subscriptions create email-worker-sub --topic=email-notifications
6. IAM Role Permissions for Pub/Sub

Ensure the service account has the correct roles and permissions assigned to it.

  • Go to IAM & Admin > IAM
  • Provide access (grant the following roles) to the service account:
  • Pub/Sub Publisher (for production)
  • Pub/Sub Subscriber (for manual testing)
  • Pub/Sub Editor (optional for development)
IAM Role Permissions
7. Handling Gmail Push Notifications with FastAPI + Pub/Sub

If you also need to consume Gmail-Watch notifications (the Gmail API can publish a Pub/Sub message every time a mailbox changes), drop the following FastAPI endpoint into the same service that processes your email-events topic. It receives the HTTP push that Cloud Pub/Sub sends to your webhook, decodes the base-64 payload, and prints the change record.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 from fastapi import FastAPI, Request, status import base64 import json app = FastAPI() @app.post("/gmail/webhook", status_code=status.HTTP_200_OK) async def gmail_webhook(request: Request): """ Endpoint for Cloud Pub/Sub push subscriptions that forward Gmail ‘watch’ notifications. Pub/Sub wraps the notification in a JSON envelope: { "message": { "data": "base64-encoded-json", "messageId": "1234567890", ... }, "subscription": "projects/-/subscriptions/your-sub" } We need to: 1. Decode the 'data' field (base-64). 2. Parse the inner JSON to read Gmail’s historyId / email metadata. """ envelope = await request.json() message = envelope.get("message", {}) encoded = message.get("data") if not encoded: # Pub/Sub might occasionally send test messages with no data. return {"status": "ignored – no data"} decoded_bytes = base64.b64decode(encoded) decoded_str = decoded_bytes.decode("utf-8") gmail_event = json.loads(decoded_str) # 👉 Replace with your own processing logic # (e.g., push historyId into another Pub/Sub topic or queue a worker) print("📬 Gmail push notification:", json.dumps(gmail_event, indent=2)) return {"status": "success"}
Note:
  • Gmail watch() expires every 7 days

  • Set a cron job or schedulercron job or scheduler to refresh it automatically.

  • You have to set up the webhook URL for push notification as below

Edit Subscription

Gmail Subscriber

When moving your Gmail Pub/Sub integration to production, follow these best practices:

  • 1. Secure Your Webhook Endpoint
    Always use HTTPS and implement proper authentication for your webhook.
  • 2. Implement Comprehensive Logging
    Track all important events in your integration.
  • 3. Create a Resilient Email Processing Pipeline
    Design your system to handle failures gracefully.

Common Challenges and Solutions

  • 1. Authentication Failures
    Challenge: OAuth2 tokens expire or become invalid. Gmail only allows API calls with user-granted scopes; access tokens expire after 1 hour.
    Solution: Implement token refresh logic and handle authentication errors gracefully. OR
    Use offline access (access_type=offline) so Google issues a refresh token.
  • 2. Push Notification Delivery Issues
    Challenge: Webhook endpoint unreachable or returning errors.
    Solution: Implement health checks and monitoring for your webhook endpoint.
  • 3. Processing High Email Volume
    Challenge: Heavy email traffic overwhelming your processing capacity.
    Solution: Implement rate limiting and batching.
  • 4. Watch Expiration (push notifications)
    Challenge: users.watch expires after ~7 days; if you forget to renew, you miss events.
    Solution: Schedule a Cloud Scheduler / cron job to renew every 6 days; persist the latest historyId so you can replay missed events with users.history.list.
  • 5. Duplicate or Out-of-Order Webhooks
    Challenge: Pub/Sub guarantees at-least-once delivery; Gmail can emit several notifications for the same change.
    Solution: Deduplicate by the combination (historyId, messageId) before processing. Use a Redis set, Firestore document, or SQL unique index as a bloom.

Conclusion

Integrating your email workflows with a Pub/Sub messaging system brings robustness, scalability, and flexibility to your communications infrastructure. By decoupling event production from email delivery, you can handle varying workloads, ensure deliverability, and add new consumers (analytics, archiving) with minimal changes.

Ready to Build Something Amazing?

Let's discuss your project and create a custom web application that drives your business forward. Get started with a free consultation today.

Call us: +1-945-209-7691
Email: inquiry@mol-tech.us
2000 N Central Expressway, Suite 220, Plano, TX 75074, United States

More Articles

Build Secure Local AI Assistants with Ollama and n8n — Private, Automated Workflows Cover Image
Oct 9th, 2025
11 min read

Building AI Assistants with Ollama and n8n — Local, Private, and Automated

Explore how Ollama and n8n power secure, on-prem AI assistants with private automation and scalable workflow orchestrati...

Moltech Solutions Inc.
Know More
Running Private LLMs Locally with Ollama — Secure, Cost-Effective AI Solutions Cover Image
Oct 7th, 2025
10 min read

Running Private LLMs Locally with Ollama: A Secure Alternative to Cloud AI

Discover how running private LLMs locally with Ollama boosts privacy, cuts costs, and accelerates AI adoption with secur...

Moltech Solutions Inc.
Know More
Embed AI in Web Apps Without Rewriting Your Stack — Custom AI Solutions & IT Consulting Cover Image
Oct 5th, 2025
9 min read

Embed AI in Web Apps Without Rewriting Your Stack | AI Solutions & Consulting

Discover how to add AI chatbots, recommendations, and analytics to your web app fast with our custom AI development and ...

Moltech Solutions Inc.
Know More
Building Conversational Web Agents with the Model Context Protocol (MCP) — AI-Powered Assistants Cover Image
Oct 3rd, 2025
10 min read

Building Conversational Web Agents with MCP: Intelligent, Secure & Scalable AI Assistants

Learn how the Model Context Protocol (MCP) standardizes AI tool integration, enabling secure, multi-agent conversational...

Moltech Solutions Inc.
Know More
Vibe Coding & AI-Assisted Development — Future of Software Engineering Cover Image
Oct 1st, 2025
9 min read

Vibe Coding & AI-Assisted Development: Risks, Benefits, and How to Get It Right

Explore how vibe coding and AI-assisted development are transforming the software industry — balancing speed, creativity...

Moltech Solutions Inc.
Know More
Kubernetes & Docker Updates 2025 — Cloud-Native Essentials Cover Image
Sep 29, 2025
10 min read

Kubernetes and Docker Updates 2025: New Features for Cloud-Native Devs

Kubernetes & Docker updates 2025: AI tools, GPU scheduling, and cloud-native workflows to cut costs, boost reliability, ...

Moltech Solutions Inc.
Know More