Email Provider Integration (Google & Outlook)
Relevant source files
The following files were used as context for generating this wiki page:
The Email Provider Integration system is a core module in Inbox Zero, designed to create a standardized interface for interacting with different email services. It abstracts the complexities of the Google (Gmail) and Microsoft (Outlook) APIs, allowing the application to perform email-related operations like fetching messages, creating drafts, and receiving real-time updates through a unified set of methods. This abstraction is crucial for features such as the AI Personal Assistant and Reply Zero that rely on consistent access to the user's inbox regardless of the underlying email provider.
The system is built around a provider pattern, with a central EmailProvider interface defining a contract for all provider-specific implementations. This ensures that the rest of the application can be developed agnostically, with the appropriate provider being instantiated at runtime based on the user's linked account.
Sources: apps/web/utils/email/provider.ts, ARCHITECTURE.md
System Architecture
The integration is architected using a factory and provider pattern. A generic EmailProvider interface defines all the necessary email operations. Concrete classes, GoogleEmailProvider and MicrosoftEmailProvider, implement this interface using their respective service-specific SDKs. The getEmailProvider factory function is responsible for creating and returning the correct provider instance based on the account's provider type (e.g., 'google' or 'microsoft').
This diagram illustrates the class structure of the email provider system.
Core EmailProvider Interface
The EmailProvider interface is the cornerstone of the integration, defining a contract that all provider implementations must adhere to. This ensures that any part of the application interacting with emails uses a consistent API.
Sources: apps/web/utils/email/provider.ts:13-73
| Method | Description |
|---|---|
getThreadMessages(threadId) | Fetches all messages within a specific thread. |
getPreviousConversationMessages(messageIds) | Retrieves messages from previous conversations with the same sender(s). |
draftEmail(message, options) | Creates a new draft email in response to a given message. |
sendMessage(messageId) | Sends a previously created draft. |
watch(labelIds) | Sets up a notification channel (webhook) to watch for changes in specified labels. |
renewWatch(labelIds) | Renews an existing notification channel. |
stopWatch() | Stops an active notification channel. |
createLabel(name) | Creates a new label in the user's inbox. |
getLabels() | Fetches all existing labels for the user's account. |
modifyMessage(messageId, options) | Modifies an existing message, such as adding or removing labels. |
getContacts() | Retrieves the user's contacts. |
createFilter(options) | Creates a new filter/rule for incoming emails. |
listFilters() | Lists all existing filters/rules. |
deleteFilter(id) | Deletes a specific filter/rule. |
updateVacation(options) | Updates the user's vacation/out-of-office responder settings. |
getVacation() | Retrieves the current vacation/out-of-office settings. |
disableVacation() | Disables the vacation/out-of-office responder. |
Provider Implementations
Google (Gmail) Provider
The GoogleEmailProvider handles all interactions with the Gmail API. It uses the @googleapis/gmail library to make API calls. A getGmailClient function initializes the client with the user's OAuth access token. Messages fetched from the API are normalized into the application's ParsedMessage format by the parseMessage function.
Key functionalities include:
- Watching for changes: The
watchmethod sets up a push notification subscription via Google Cloud Pub/Sub. - Data Parsing: It parses complex Gmail API message payloads, including headers, body content (HTML and plain text), and attachments, into a structured
ParsedMessageobject.
Sources: apps/web/utils/email/google.ts
Microsoft (Outlook) Provider
The MicrosoftEmailProvider integrates with the Microsoft Graph API to manage Outlook inboxes. It utilizes the @microsoft/microsoft-graph-client library, and the getMicrosoftGraphClient function prepares an authenticated client instance. Like the Google provider, it has a parseMessage function to transform API responses into the standard ParsedMessage format.
Key functionalities include:
- Watching for changes: The
watchmethod callscreateSubscriptionto set up a webhook subscription directly with the Microsoft Graph API, which sends notifications for new mail. - Subscription Management: It includes logic to renew (
renewWatch) and delete (stopWatch) webhook subscriptions to maintain real-time updates.
Sources: apps/web/utils/email/microsoft.ts
Authentication and Setup
Authentication for both providers is handled via OAuth 2.0. The application requires specific permissions (scopes) to access and manage a user's email. These are configured in the Google Cloud Console and Microsoft Azure Portal.
Sources: README.md
Google OAuth Configuration
To connect a Google account, the application requires an OAuth Client ID and Secret. The following scopes must be enabled in the Google Cloud project to grant the necessary permissions.
| Scope | Description |
|---|---|
https://www.googleapis.com/auth/userinfo.profile | View your basic profile info. |
https://www.googleapis.com/auth/userinfo.email | View your email address. |
https://www.googleapis.com/auth/gmail.modify | Read, compose, send, and permanently delete all email. |
https://www.googleapis.com/auth/gmail.settings.basic | Manage basic mail settings. |
https://www.googleapis.com/auth/contacts | See, edit, download, and permanently delete contacts. |
Sources: README.md:104-110
Microsoft OAuth Configuration
For Microsoft accounts, an application must be registered in Azure Active Directory to obtain a Client ID and Secret. The following Microsoft Graph API permissions are required.
| Permission | Description |
|---|---|
openid | Sign users in. |
profile | View users' basic profile. |
email | View users' email address. |
User.Read | Read user's profile. |
offline_access | Maintain access to data you have given it access to. |
Mail.ReadWrite | Read and write user mail. |
Mail.Send | Send mail as a user. |
Mail.ReadBasic | Read user basic mail properties. |
Mail.Read | Read user mail. |
Mail.Read.Shared | Read user and shared mail. |
MailboxSettings.ReadWrite | Read and write user mailbox settings. |
Contacts.ReadWrite | Read and write user contacts. |
Sources: README.md:162-175
Real-time Notifications via Webhooks
Webhooks are used to receive real-time notifications about new emails, enabling immediate processing by the AI assistant. The implementation differs between Google and Microsoft.
Google Pub/Sub Webhook Flow
Google's Gmail API does not send webhook notifications directly. Instead, it publishes messages to a Google Cloud Pub/Sub topic. The application creates a push subscription to this topic, which forwards notifications to a designated API endpoint.
The flow is as follows:
- A new email arrives in the user's Gmail inbox.
- The Gmail API publishes a notification to the configured Pub/Sub topic.
- The Pub/Sub service pushes this notification to the application's
/api/google/webhookendpoint. - The webhook handler validates a verification token, decodes the Base64-encoded message data, and extracts the user's email and new history ID.
- It then calls
processGmailWebhookto fetch the new email data and trigger further application logic.
This diagram shows the sequence of events for a Google webhook notification.
Outlook Webhook Flow
Microsoft Graph allows for direct webhook subscriptions. The application subscribes to new mail notifications, and Microsoft sends a POST request to the application's endpoint when a new email arrives.
The flow is as follows:
- Subscription: The application creates a subscription with Microsoft Graph, providing the
/api/outlook/webhookendpoint URL. Microsoft sends a validation request with avalidationToken, which the app must return to confirm the endpoint. - Notification: When a new email arrives, Microsoft Graph sends a POST request to the webhook URL with a payload containing information about the event.
- Processing: The webhook handler receives the notification, validates it, and calls
processOutlookWebhookto fetch the new email and initiate processing.
This diagram shows the sequence of events for an Outlook webhook notification.
Conclusion
The Email Provider Integration module is a foundational component of Inbox Zero that enables seamless connectivity with both Google and Microsoft email services. By abstracting provider-specific details behind a common EmailProvider interface, it simplifies development and allows features to be built agnostically. The use of real-time webhook notifications ensures that the application can react instantly to new emails, which is essential for its core AI-powered automation and organization capabilities. This robust and extensible architecture makes it possible to support additional email providers in the future with minimal changes to the core application logic.