Every day, your inbox likely receives numerous emails with critical attachments like invoices, contracts, resumes, or digital assets. Manually downloading these attachments and organizing them in Google Drive can be tedious and error-prone. In this tutorial, you will learn how to automate this entire process using n8n, so that attachments from your Gmail arrive automatically saved and organized in Google Drive, with optional team notifications on Discord.
You’ll build a workflow step-by-step, starting from handling a single attachment to managing multiple attachments dynamically. This modular design approach aligns well with how branching works in n8n workflows, enabling flexible and maintainable automation.
Prerequisites
Before you begin, ensure you have the following ready:
- An n8n instance (cloud or self-hosted)
- Access to a Gmail account with emails containing attachments
- Google Drive account with a folder for saving attachments
- Discord webhook URL (optional, for notifications)
- Configured credentials in n8n for Gmail, Google Drive, and Discord nodes
Step 1: Setting Up the Gmail Trigger Node
The first step is to detect incoming emails in Gmail and download their attachments.
- In your n8n workflow canvas, add a new node and select
Gmail→Trigger. - Choose the event
Message Receivedto listen for new incoming emails. - Select or configure your Gmail account credentials.
- Disable the
Simplifyoption to get full email data. - Enable the option
Download Attachmentsto automatically download attachments. - Optionally, set
Attachment Prefixtoattachment_(default) to prefix downloaded attachment keys. - Click
Fetch Test Eventto pull a recent email with an attachment for testing.
This node will check your inbox every minute and output emails with their attachments as binary data.
Step 2: Filtering Emails That Have Attachments
Not every email will have attachments, so you need to filter out emails without them.
Add a
Filternode after the Gmail trigger.Configure the filter to check if the binary data exists:
- Switch to Expression mode.
- Use the expression:
{{$binary !== undefined}}
Rename this node to
Filter Emails With Attachments.Connect the Gmail trigger node output to this filter node.
This ensures only emails containing attachments proceed through the workflow.
Step 3: Uploading Attachments to Google Drive (Single Attachment Case)
To upload attachments to Google Drive, add a Google Drive node.
Add a
Google Drivenode.Set
ResourcetoFileandOperationtoUpload.Select your Google Drive credentials.
For
Input Data Field Name, enterattachment_0(for the first attachment).For
File Name, use an expression to create a meaningful file name. For example:{{$json["subject"] + "_first_attachment_upload"}}Select the
Parent Folderas your desired Google Drive folder (e.g.,Hands-On Labs).Connect the
Filter Emails With Attachmentsnode output to this Google Drive node.Execute the workflow to test uploading the first attachment.
Note: This method works only for emails with a single attachment named attachment_0.
For more details on the Google Drive node capabilities, refer to the Google Drive node documentation which explains all upload options and settings.
Step 4: Sending Notifications to Discord
Optionally, notify your team on Discord when an attachment is uploaded.
Add a
Discordnode.Set
OperationtoSend Message.Use your Discord Webhook URL for authentication.
Set the message text, e.g.:
A new important attachment has been uploaded to Google Drive. Subject: {{$node["Filter Emails With Attachments"].json["subject"]}}Connect the Google Drive node output to the Discord node.
Test the workflow by running it and verifying a Discord message appears.
Step 5: Handling Multiple Attachments with SplitOut Node
Emails often have multiple attachments. The previous workflow only handles one attachment, so you need to split multiple attachments into separate items.
- Add a
SplitOutnode after theFilter Emails With Attachmentsnode. - Set the property to split on as
{{$binary}}. - This node will split the attachments from a single email into multiple separate items.
- Connect the output of the
SplitOutnode to theGoogle Drivenode.
This approach leverages n8n's ability to handle multiple items efficiently. For optimizing workflows that process multiple items or sub-workflows, consider reviewing best practices in how to optimize workflows in n8n for faster, scalable automations.
Step 6: Dynamically Setting the Input Data Field Name for Google Drive
The Google Drive node expects the binary data field name for each attachment, which varies (attachment_0, attachment_1, etc.). To handle this dynamically:
In the Google Drive node, enable Expression mode for
Input Data Field Name.Use the following expression to dynamically select the current attachment key:
{{ Object.keys($binary)[0] }}
This accesses the keys of the binary data object and picks the first one dynamically for each item.
Step 7: Creating a Dynamic File Name Combining Email Subject and Attachment Name
To better organize files, dynamically name uploaded files with the email subject and original attachment filename.
In the Google Drive node's
File Namefield, enable Expression mode.Use this expression:
{{ $item(0).$node["Filter Emails With Attachments"].json["subject"] + "_" + Object.values($binary)[0].fileName }}
Explanation:
$item(0).$node["Filter Emails With Attachments"].json["subject"]fetches the email subject from the previous filter node.Object.values($binary)[0].fileNamefetches the original filename of the attachment from the binary data.
This results in filenames like:
FWD: Project Update_attachment1.pdf
Step 8: Finalizing the Workflow and Testing
Your final workflow should look like this:
Gmail Trigger(Message Received with attachments)Filter Emails With AttachmentsSplitOut(split multiple attachments)Google Drive(upload each attachment dynamically)Discord(optional notification)
Test the workflow:
- Clear previous executions.
- Run the workflow manually or wait for new emails.
- Verify attachments are uploaded correctly with dynamic names.
- Check Discord for notification messages.
If you want to handle errors gracefully in this workflow, especially when working with sub-workflows or multiple nodes, consider exploring master error handling in n8n to make your automation more robust.
Troubleshooting and Common Mistakes
- Attachments Not Uploading: Ensure
Download Attachmentsis enabled in the Gmail trigger node. - Google Drive Upload Errors: Check that the
Input Data Field Nameis set dynamically using the expressionObject.keys($binary)[0]to avoid hardcoding. - Undefined Subject in File Name: When using the
SplitOutnode, the email subject is lost in split items. Reference the subject from the filter node using$item(0).$node["Filter Emails With Attachments"].json["subject"]. - Discord Messages for Each Attachment: If you want a single notification for multiple attachments, consider aggregating messages using the
MergeorCodenode before the Discord node. The n8n Merge node documentation provides useful insights on combining data streams. - Permissions Issues: Make sure your Google Drive and Gmail credentials have the necessary scopes and permissions to access and upload files.
Additional Tips
- Use folder names based on email metadata (e.g., sender or date) by adding more nodes or expressions.
- Integrate AI or logic nodes to classify attachments or filter by sender.
- Use the official n8n Gmail node documentation and Google Drive node documentation for deeper customization.
Quick Reference Cheat Sheet
| Step | Node Type | Key Settings / Expressions |
|---|---|---|
| Detect new emails | Gmail Trigger |
Event: Message Received, Download Attachments: true |
| Filter emails | Filter |
Expression: {{$binary !== undefined}} |
| Split attachments | SplitOut |
Property to split: {{$binary}} |
| Upload attachment | Google Drive |
Input Data Field Name: {{ Object.keys($binary)[0] }} |
File Name: {{ $item(0).$node["Filter Emails With Attachments"].json["subject"] + "_" + Object.values($binary)[0].fileName }} |
||
| Notify team | Discord |
Message: Custom text with subject or file info |
By following this tutorial, you will have a robust n8n workflow that automates saving Gmail attachments to Google Drive, saving you time and reducing manual errors. As shown in the video above, this workflow can be extended and customized further for your specific use cases. For more details, refer to the official n8n documentation.