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.

Logo Image

Introduction

“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
Logo Image
  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

Gmail’s API with pub/sub notifications provides a powerful way to integrate email functionality into your applications. Let’s 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”)
Logo Image

2. Enable Required APIs

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

  • Gmail API
  • Cloud Pub/Sub API
Logo Image


Logo Image

3. Create Service Account and Credentials:

  • Go to IAM & Admin > Service Accounts > Create Service Account
Logo Image


Logo Image
  • Grant access to this service account with the role: Pub/Sub Publisher.
  • Go to the Keys tab and create a new key.
Logo Image
  • Select Key type, we choose JSON
Logo Image
  • 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.

Logo Image
  • 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.)
Logo Image
  • 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.

Logo Image


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)
Logo Image

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

Logo Image


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.

Contact Information

Say something to start a live chat!

+1 (732) 552-8682
+1 (732) 552-8682
2000 N Central ExpresswaySuite 220 Plano, TX 75074United States
inquiry@mol-tech.us

More Articles

Simplifying Software Development with Interfaces in C#
Jul 12, 2025
6 min read

Simplifying Software Development with Interfaces in C#

Building software that is easy to manage, grow, and update is a goal for every developer. Interfaces are a key concept that helps make this possible. Interface as a contract or blueprint that a class must follow. It doesn’t tell the class how to do something, just what it needs to do. This makes your code flexible, organized, and easier to work with.

Moltech Solution Inc.
Read More
Threads or Tasks? The Parallel Programming in C#
Jul 10, 2025

Threads or Tasks? The Parallel Programming in C#

In the world of modern application development, parallel programming is a must to make the most of multi-core processors. For C# developers, the choice often boils down to using Threads or Tasks. Both are key to achieving concurrency and parallelism, but knowing what sets them apart, as well as their respective advantages and limitations, is crucial for mastering efficient C# programming.

Moltech Solution Inc.
Read More
Middleware in .NET: Enhancing Request Handling with Custom Logic
Jul 08, 2025

Middleware in .NET: Enhancing Request Handling with Custom Logic

Middleware is a crucial component of web development in .NET Core, managing how HTTP requests and responses are processed. It consists of software components the ASP.NET Core request pipeline that handle various aspects of the request/response lifecycle, such as :

Moltech Solution Inc.
Read More
Events and Delegates in C#
Jun 25, 2025

Events and Delegates in C#

In C#, events and delegates are key to creating flexible and responsive programs. Delegates, function similarly to method pointers, facilitating the calling of one method from another part of your code. Events employ delegates to inform other components of the program when an action occurs, such as a button being clicked. Utilizing events and delegates together enables the construction of programs that not only react to user interactions but also manage background tasks, thereby enhancing code organization and maintainability.

Moltech Solution Inc.
Read More
CORS (Cross-Origin Resource Sharing) in C# and .NET
Jun 19, 2025

CORS (Cross-Origin Resource Sharing) in C# and .NET

CORS, or Cross-Origin Resource Sharing, is a crucial security mechanism that enables web applications to request resources from a domain other than their own.

Moltech Solution Inc.
Read More
Enumrable vs Collection Cover IMG
Jun 14, 2025

Understanding IEnumerable vs. ICollection in .NET

If you’re working with collections in C#, you’ve probably come across IEnumerable and ICollection. But what exactly are they? What are the differences between them, and how do you know which one to use?

Moltech Solution Inc.
Read More