How to Auto-Save Gmail Attachments to Google Drive | n8n AI Automation Tutorial

Learn How to Auto-Save Gmail Attachments to Google Drive | n8n AI Automation Tutorial in this comprehensive n8n tutorial. Step-by-step guide with video, code examples, and expert tips from the n8n Zero to Hero course.

Table of Contents

Overview

This lesson is part of the n8n AI Automation - Zero to Hero course, Section: 5. Hands-On Projects.

Watch the video above for the full tutorial, or read the written guide below.

What Is the Gmail-to-Google Drive Attachment Workflow in n8n?

The Gmail Trigger node anchors an n8n automation that saves email attachments directly to a Google Drive folder and fires a Discord team notification. The four-node chain runs: Gmail Trigger polling the inbox every minute, a Filter node discarding emails with no binary data, a Google Drive upload node, and a Discord webhook node. Emails with multiple attachments need a fifth node, Split Out, to fan each binary field into its own item before the upload.

How to Build the Gmail Attachment Auto-Save Workflow in n8n

The Gmail Trigger, Filter, Split Out, Google Drive, and Discord nodes chain together to route every email attachment into a designated Drive folder automatically. Configure the Gmail Trigger to download attachments, gate with a binary-exists Filter, fan multi-attachment emails through Split Out, use two dynamic expressions in the Google Drive node to resolve field names and filenames, and send a Discord webhook notification on each upload.

The Gmail Trigger node starts with the "On Message Received" event. Disable the "Simplify" toggle so the full email payload is available, then enable "Download Attachments" with the default prefix attachment_. That prefix is critical: n8n names every binary field attachment_0, attachment_1, and so on, and every downstream expression depends on that naming convention. Add a Filter node next with the condition set to expression $binary, type Object, condition Exists. This gate silently drops plain-text emails before they reach the upload node.

The Split Out node solves the multiple-attachment problem. A Gmail email with nine attachments arrives in n8n as one item carrying nine binary fields, not nine separate items. Set the Split Out field to $binary so it outputs nine individual items. In the Google Drive node (resource: File, operation: Upload), replace the hardcoded attachment_0 in "Input Data Field Name" with the expression {{$binary.keys().first()}}. This dynamically returns the correct binary key for whichever item is currently being processed. For "File Name," use {{$("Filter emails with attachments").item.json.subject}}_{{$binary.values().first().fileName}} to combine the email subject with each attachment's original filename stored in the binary metadata. Select the target parent drive and destination folder to complete the upload configuration.

The Discord node completes the chain using a webhook credential, not a bot token. Reference the email subject from the Filter node in the message body. Because nine attachments produce nine separate Discord messages, place an Aggregate or Code node upstream of Discord to collapse them into a single team notification.

Key Takeaways

  • The Gmail Trigger requires "Download Attachments" enabled and the attachment_ prefix set; without it, no binary data flows to any downstream node.
  • The Filter node checks $binary Object Exists to catch any email carrying at least one attachment, regardless of the exact field name or total attachment count.
  • The Split Out node set to $binary converts one multi-attachment email item into N separate items so the Google Drive node runs once per file instead of erroring after the first.
  • {{$binary.keys().first()}} dynamically resolves each item's binary field name, replacing the hardcoded attachment_0 that causes upload failures for every file beyond the first.
  • {{$binary.values().first().fileName}} reads the original attachment filename from the binary metadata, enabling organized and searchable filenames in Google Drive rather than generic hardcoded strings.

Next Steps

Continue your n8n journey with the full n8n AI Automation - Zero to Hero course.

Frequently Asked Questions

The Google Drive node fails because "Input Data Field Name" defaults to the hardcoded string `attachment_0`. A multi-attachment email arrives in n8n as a single item with binary fields named `attachment_0` through `attachment_8`. The node uploads the first file, then errors on every subsequent item because their binary keys do not match `attachment_0`. The fix is a Split Out node (field: `$binary`) upstream plus the dynamic expression `{{$binary.keys().first()}}` in the field name setting.

The Split Out node converts one n8n item that carries multiple binary fields into individual items, one per binary field. Set "Field" to `$binary` so n8n reads the attachment object and outputs a separate item for each attachment. An email with nine attachments produces nine items, each holding one binary field, so the Google Drive node can upload each file in its own execution instead of failing after the first attachment_0 match.

The expression `{{$binary.values().first().fileName}}` retrieves the original filename stored by the Gmail Trigger. `$binary.values()` returns an array of the binary field objects on the current item, `.first()` selects the first one, and `.fileName` reads the filename property. Combine it with the email subject for organized Drive storage: `{{$('Filter emails with attachments').item.json.subject}}_{{$binary.values().first().fileName}}`.

The Filter node checks `$binary` as an Object Exists because the binary field names change with attachment count, starting at `attachment_0` and incrementing. Checking the `$binary` object itself catches any email carrying at least one attachment regardless of how many files it includes. Checking a hardcoded field like `attachment_0` would only match that exact field and could silently pass or drop emails incorrectly once the Split Out node reorganizes the binary structure downstream.

Dheeraj Sharma

Dheeraj Sharma

AI Systems Builder
Creator of the n8n Zero to Hero course (42 lessons, 31+ hours). I help solopreneurs build AI systems that grow revenue without growing workload.

Get the n8n Mastery Bundle

All workflows, cheat sheets, and premium resources from the entire course in one package.

Get Premium Resources