Multi-Account Gmail Polling and Centralized Data Storage - n8n Workflow

Use this advanced n8n workflow to poll multiple Gmail accounts hourly, normalize incoming messages, store unified email data in n8n Data Tables, and send immediate Discord alerts for new activity.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Agencies managing communications across multiple client or burner accounts.
Developers requiring unified logging and monitoring for many email inboxes.
Anyone needing an advanced example of using the n8n node for dynamic credential management.
Users looking for high-utility n8n templates involving Gmail automation and structured data storage.

Overview

Managing new messages across numerous Gmail accounts can be highly complex and often requires manual credential switching. This n8n workflow solves this by utilizing n8n Data Tables to store account credentials and state data (like last_polled).

Starting with an n8n trigger, the automation iterates through every account, dynamically injecting the necessary Gmail OAuth2 credentials using the powerful Run Node With Credentials n8n node. It fetches only new emails since the last successful poll, normalizes the raw message payload, and saves a standardized version into a master Data Table, preventing duplicates and establishing a single source of truth.

This architecture ensures that even complex, multi-credential tasks are handled seamlessly within a single n8n workflow, providing efficient polling and a summary notification via Discord upon completion.

How it Works

This highly advanced n8n workflow operates in a structured, looping fashion:


  1. Scheduled Start: The automation begins with a Schedule n8n trigger, configured to fire hourly to initiate the polling cycle.

  2. Fetch Accounts: The workflow retrieves a list of all target email accounts, including their respective n8n credential IDs and the lastpolled timestamp, from the coldemailaccounts n8n Data Table.

  3. Iteration: A Split In Batches n8n node loops over each email account retrieved.

  4. Calculate Polling Window: A Code n8n node calculates the exact time window for polling by converting the account's lastpolled time and the current time ($now) into Epoch seconds, defining the after: and before: search filters for Gmail.

  5. Dynamic Gmail Polling: The crucial Run Node With Credentials X n8n node executes an internal Gmail n8n node. It injects the account's unique OAuth credentials dynamically and runs the getAll operation using the calculated time window, ensuring only new, relevant emails are fetched.

  6. Conditional Processing: An If n8n node checks if any new emails were returned.

If Yes: The new emails are passed to a second Code n8n node (Email Normalization) that transforms the complex Gmail API response into a clean, standardized JSON object. This data is then upserted (inserted or updated) into the central All Emails n8n Data Table using the message ID to guarantee deduplication. Finally, the account's lastpolled timestamp in the source Data Table is updated.
If No: The workflow skips the normalization and upsert steps and immediately updates the last
polled timestamp for that account, marking the successful completion of the poll cycle.

  1. Notification: Once the loop concludes, the accumulated data (containing details of newly processed emails) is counted using a Set n8n node. A Discord n8n node then sends a final summary message, indicating how many new emails were successfully processed across all monitored accounts. This robust approach is a prime example of effective multi-step n8n templates.

Installation Guide

To set up this n8n workflow, follow these steps:


  1. Import: Copy the provided JSON code and import it directly into your n8n instance.

  2. Credentials Setup: You will need two main credentials:

Gmail OAuth2: Set up one Gmail credential for each account you intend to poll. These IDs are referenced in your Data Table.
Discord Bot API: Configure your Discord Bot credential to allow the workflow to post notifications.

  1. Data Table Configuration: This n8n workflow requires two specific Data Tables:

coldemailaccounts: This table must store rows for each account, including columns like credid (the ID of the corresponding Gmail OAuth2 credential), email, and lastpolled (a Date/Time field).
All Emails: This table is the unified data sink where normalized emails will be stored. Ensure it has columns matching the standardized output (e.g., messageid, emaildata).

  1. Activation: Ensure all n8n nodes, especially the Data Table nodes, are correctly linked to your newly created tables and credentials, then activate the schedule n8n trigger.

Node Details

Schedule Trigger (n8n trigger): The starting point. Configured to run periodically (e.g., every hour) to maintain continuous email polling.
Get All Email Accounts (Data Table n8n node): Reads all rows from the coldemailaccounts Data Table, providing the necessary credentials and state data for the loop.
Loop Over Items (Split In Batches n8n node): Ensures that the subsequent logic is executed sequentially for each email account defined in the Data Table.
Code in JavaScript (Code n8n node): Calculates the exact polling window (after and before timestamps) using the lastpolled value of the current item.
Run Node With Credentials X (Custom n8n node): This specialized n8n node dynamically loads the Gmail OAuth2 credential ID stored in the current Data Table item and executes an internal Gmail n8n node to fetch messages, making the n8n workflow highly scalable.
Email Normalization (Code n8n node): A critical custom scripting step that standardizes the raw data received from the Gmail n8n node (parsing headers, extracting body text/HTML, and listing attachments) into a clean, unified JSON structure.
upsert emails (Data Table n8n node): Writes the normalized email data to the All Emails master table. It uses the upsert operation and matches on messageid to prevent the insertion of duplicate emails.
Update lastpolled / Update lastpolled1 (Data Table n8n node): These n8n nodes ensure that the coldemailaccounts table is updated with the current timestamp, maintaining accurate state management for the next scheduled n8n trigger run.


  • Send a message (Discord n8n node): Sends an alert to a specific Discord channel, summarizing the total count of new emails processed during the current run of the n8n workflow.

Related n8n Workflows

Free

Nodes: 10 Nodes
Updated: December 26 2025
View all
Created by

Building high-end agents and systems for businesses around the world.

Featured*