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

Automate saving Gmail attachments to Google Drive using n8n. Learn step-by-step to filter emails, download attachments, and upload files automatically.

Table of Contents

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.

  1. In your n8n workflow canvas, add a new node and select GmailTrigger.
  2. Choose the event Message Received to listen for new incoming emails.
  3. Select or configure your Gmail account credentials.
  4. Disable the Simplify option to get full email data.
  5. Enable the option Download Attachments to automatically download attachments.
  6. Optionally, set Attachment Prefix to attachment_ (default) to prefix downloaded attachment keys.
  7. Click Fetch Test Event to 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.

  1. Add a Filter node after the Gmail trigger.

  2. Configure the filter to check if the binary data exists:

    • Switch to Expression mode.
    • Use the expression: {{$binary !== undefined}}
  3. Rename this node to Filter Emails With Attachments.

  4. 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.

  1. Add a Google Drive node.

  2. Set Resource to File and Operation to Upload.

  3. Select your Google Drive credentials.

  4. For Input Data Field Name, enter attachment_0 (for the first attachment).

  5. For File Name, use an expression to create a meaningful file name. For example:

    {{$json["subject"] + "_first_attachment_upload"}}
    
  6. Select the Parent Folder as your desired Google Drive folder (e.g., Hands-On Labs).

  7. Connect the Filter Emails With Attachments node output to this Google Drive node.

  8. 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.

  1. Add a Discord node.

  2. Set Operation to Send Message.

  3. Use your Discord Webhook URL for authentication.

  4. Set the message text, e.g.:

    A new important attachment has been uploaded to Google Drive. Subject: {{$node["Filter Emails With Attachments"].json["subject"]}}
    
  5. Connect the Google Drive node output to the Discord node.

  6. 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.

  1. Add a SplitOut node after the Filter Emails With Attachments node.
  2. Set the property to split on as {{$binary}}.
  3. This node will split the attachments from a single email into multiple separate items.
  4. Connect the output of the SplitOut node to the Google Drive node.

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:

  1. In the Google Drive node, enable Expression mode for Input Data Field Name.

  2. 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.

  1. In the Google Drive node's File Name field, enable Expression mode.

  2. 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].fileName fetches 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:

  1. Gmail Trigger (Message Received with attachments)
  2. Filter Emails With Attachments
  3. SplitOut (split multiple attachments)
  4. Google Drive (upload each attachment dynamically)
  5. 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 Attachments is enabled in the Gmail trigger node.
  • Google Drive Upload Errors: Check that the Input Data Field Name is set dynamically using the expression Object.keys($binary)[0] to avoid hardcoding.
  • Undefined Subject in File Name: When using the SplitOut node, 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 Merge or Code node 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


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.

Frequently Asked Questions

Set the Gmail Trigger node event to 'Message Received', disable 'Simplify', and enable 'Download Attachments' to capture emails with attachments.

Use the expression `{{$binary !== undefined}}` in a Filter node to allow only emails with binary attachment data to pass through.

Loop through each attachment binary data in the workflow and use the Google Drive node with dynamic 'Input Data Field Name' to upload all files.

Yes, by setting the 'Parent Folder' parameter in the Google Drive node, you can save attachments directly into chosen folders.

Yes, you can add a Discord node with a webhook URL to send notifications after attachments are successfully uploaded.

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