1. Opening Problem Statement
Meet Sarah, a busy marketing manager who receives dozens of emails every day, many with crucial attachments like client contracts, campaign assets, and reports. Sarah spends about an hour daily downloading these attachments manually from Gmail and then uploading them to the right folders in Google Drive. She often misplaces files or wastes time renaming files to fit her system. This repetitive, error-prone process drains her productivity, costing her precious time that could be spent on strategic tasks.
This exact pain point is what our automation addresses using n8n. By automatically extracting attachments from emails and saving them to Google Drive with a smart, clear naming convention, Sarah can reclaim her day and focus on what matters most.
2. What This Automation Does
When this workflow runs, it listens for any new email arriving in Gmail that includes an attachment. Then it:
- Downloads each attachment from the email automatically.
- Separates multiple attachments into individual files.
- Renames each file by appending the sender’s email address to the file name for easy identification.
- Uploads each renamed file into your Google Drive’s root folder.
- Keeps files organized and prevents overwriting by unique naming.
- Saves Sarah an estimated 5+ hours per week by automating this routine.
3. Prerequisites ⚙️
- Gmail account with API access configured (for the Gmail Trigger node). 📧
- Google Drive account with API credentials set up in n8n. 📁
- n8n account or self-hosted instance to create and deploy this workflow. 🔑
- Basic permissions to connect Gmail and Google Drive via OAuth2 in n8n.
4. Step-by-Step Guide
Step 1: Set Up Gmail Trigger Node to Detect Attachments
Navigate to the n8n editor and add a new node. Search for and select Gmail Trigger. Set this trigger to watch your inbox for new emails containing attachments.
In the node parameters, under Filters, enter the Gmail search query has:attachment. This ensures it only triggers on emails with attachments.
Enable downloadAttachments to automatically fetch attachments.
Set the poll frequency to every minute to check for new emails regularly.
You should see the node connected and ready, waiting to catch qualifying emails.
Common mistake: Forgetting to enable attachment download will cause the workflow to miss files.
Step 2: Extract Each Attachment as Separate Items
Add a Function Node after the Gmail Trigger. This custom JavaScript code processes each email’s attachments, separating each one into its own item for further handling.
Copy and paste the following code into the Function Node’s editor:
let results = [];
for (item of items) {
for (key of Object.keys(item.binary)) {
results.push({
json: {
fileName: item.binary[key].fileName
},
binary: {
data: item.binary[key],
}
});
}
}
return results;This loops through all binary attachments in the incoming email and creates new output items for each, preserving the file name and data.
Outcome: Each attachment is now isolated and ready for upload.
Common mistake: Not adjusting the code if the attachment field names change in Gmail’s response.
Step 3: Upload Attachments to Google Drive
Add a Google Drive Node next. Set it to upload files.
Configure the file name dynamically using an expression to keep the original file extension but append the sender’s email for traceability. Use this expression:
= {{ $json.fileName.split(".")[0] + "-" + $('Trigger - New Email').item.json.from.value[0].address + "." + $json.fileName.split(".")[1] }}Choose the folder as your root or specify one if you want organization.
Outcome: Every attachment is uploaded with a unique identifiable name, preventing overwrites.
Common mistake: Not enabling the proper drive ID or folder can lead to uploads failing or files landing in unexpected places.
5. Customizations ✏️
- Change upload folder: In the Google Drive node, change the
folderIdto a specific folder ID to organize files by project or client. - Adjust file naming convention: Modify the expression in the Google Drive node to include the email subject or date for even better sorting.
- Add email filtering: In the Gmail Trigger node, refine the
qfilter to only trigger on emails from specific senders or with specific subject keywords. - Handle large files differently: Implement additional logic to skip large attachments or notify you via email if files exceed a threshold.
6. Troubleshooting 🔧
Problem: “No attachments found despite new emails arriving”
Cause: The Gmail search query is incorrect or missing has:attachment.
Solution: Double-check and ensure the filter includes has:attachment in the Gmail Trigger node filters.
Problem: “Error uploading to Google Drive”
Cause: Misconfigured Google Drive node credentials or invalid folder ID.
Solution: Verify your Google Drive credentials, ensure OAuth tokens are valid, and confirm the folder exists and your service account has access.
7. Pre-Production Checklist ✅
- Verify Gmail Trigger node successfully connects and detects emails with attachments.
- Test the Function node’s output to confirm attachments separate properly.
- Ensure Google Drive uploads files with the expected names and in the correct folder.
- Perform a test email send with multiple attachments for real-world simulation.
- Backup your workflow export before activating live.
8. Deployment Guide
Activate the workflow in n8n to start automated attachment processing. Monitor initial runs via the Executions tab.
Set up error notifications using n8n’s built-in alerting or connect with Slack or email alerts if needed.
This automation is ideal for individuals or teams handling many email attachments daily and looking for a low-code, reliable solution.
10. Conclusion
By implementing this n8n workflow, you’ve automated the tedious task of manually downloading and organizing Gmail attachments, saving potentially 5+ hours weekly. Your files now upload securely and uniquely named to Google Drive, reducing errors and improving productivity.
Next, consider expanding this automation to categorize attachments by client, notify teams automatically via messaging apps, or integrate with document management systems for further efficiency gains.
Happy automating!