1. Opening Problem Statement
Meet Sarah, a busy HR assistant at a medium-sized company. Every day, Sarah receives hundreds of emails, many with PDF attachments that are important but mixed in with less relevant documents. For example, she needs to collect all employee payslips from emails and upload them into a designated Google Drive folder for payroll processing. Without automation, Sarah spends hours daily manually opening each email, downloading PDFs, reading them to verify if they are payslips, and then uploading the correct ones. This tedious process is prone to human error—missing files or wrongly sorted documents—and wastes valuable time that could be spent on strategic HR tasks.
This is exactly where this specialized n8n workflow comes in — it automates the entire process: from detecting incoming email attachments to intelligently identifying the relevant PDFs using OpenAI’s natural language processing and finally uploading matched documents to the correct Google Drive folder. It’s a dramatic time saver and accuracy booster tailored for scenarios like Sarah’s payroll document handling.
2. What This Automation Does
Let’s break down what happens when this workflow runs on your n8n automation server:
- Trigger on Email Receipt: Automatically activates whenever a new email arrives in your Gmail account.
- Attachment Iteration: Processes each attachment individually to handle multiple files per email smoothly.
- PDF Verification: Checks attachment file types to ensure only PDFs go forward for content analysis.
- Text Extraction: Extracts all textual content from PDFs to prepare for AI evaluation.
- OpenAI Content Matching: Sends extracted text to OpenAI to determine if it contains a specific keyword, like “payslip,” effectively filtering relevant documents.
- Upload to Google Drive: Successfully identifies the PDFs matching your criteria and uploads them to a preset folder on Google Drive automatically.
By streamlining these steps, this workflow saves you potentially hours of manual review every day, reduces attachment mismanagement, and maintains a well-organized digital filing system with minimal effort.
3. Prerequisites ⚙️
- 📧 Gmail account: Set up with OAuth2 credentials for n8n to access your inbox securely.
- 🔐 OpenAI API key: To use AI-powered content matching.
- 📁 Google Drive account: With a folder URL ready where matched PDFs will be uploaded.
- ⚙️ n8n account or self-hosted instance: To create, test, and run the workflow.
Optionally, you can self-host n8n for full control and security. Check out services like Hostinger for simple hosting options.
4. Step-by-Step Guide to Build This Workflow
Step 1: Set Up Gmail Trigger Node
Click + Add Node → Search for Gmail Trigger and add it to your canvas.
Configure it to listen for new emails:
– Select your Gmail OAuth2 credentials.
– Set to poll every minute for new emails.
– Enable Download Attachments option to handle the email files.
You should now have a live trigger that starts the workflow whenever you receive emails with attachments.
Common mistake: Forgetting to enable attachment downloads results in no files to process.
Step 2: Create Configuration Node (Set Node)
Add a Set node named Configure.
Define key parameters:
– Match on: Keyword to search for in PDF content (e.g., “payslip”).
– Google Drive folder to upload matched PDFs: Paste your target folder link.
– maxTokenSize: Set to 4000 (to limit AI input token size).
– replyTokenSize: Set token limit for AI response (50 recommended).
This node centralizes all variables, making updates easier later.
Expected outcome: The workflow will reference this node’s values dynamically.
Step 3: Check for Attachments
Add an If node titled Has attachments?
Set condition to check if the email has any binary data.
– Condition: {{$node["On email received"].item.binary.isNotEmpty()}} equals true.
If no attachments are found, the workflow stops here.
Step 4: Iterate Over Attachments with Code Node
Add a Code node named Iterate over email attachments.
Paste this JavaScript code to split attachments individually for processing:
// Iterate over all binary attachments
let results = [];
for (const item of $input.all()) {
for (key of Object.keys(item.binary)) {
results.push({
json: {},
binary: {
data: item.binary[key],
}
});
}
}
return results;
> This ensures every attachment is processed one at a time.
Step 5: Check If Attachment is PDF
Add an If node Is PDF.
Use condition: Check if {{$binary.data.fileExtension}} equals pdf.
If not PDF, the flow continues to a No Op node Not a PDF that ends processing for that file.
Step 6: Read PDF Text Content
Add the Read PDF node; connect from the Is PDF true branch.
This extracts textual content from the PDF, making it available for analysis.
Step 7: Check PDF Text Length Within OpenAI Token Limit
Add an If node named Is text within token limit?
Condition:
{{$json.text.length() / 4 <= $node["Configure"].json.maxTokenSize - $node["Configure"].json.replyTokenSize}}
If true, continue; if false, route to Ignore large PDFs no-op node.
Step 8: Use OpenAI to Match PDFs
Add the OpenAI node called OpenAI matches PDF textual content.
Setup prompt to ask OpenAI if the text looks like the keyword set in Configure.
Example prompt:
Does this PDF file look like a {{ $("Configure").first().json["Match on"] }}? Return "true" if it is and "false" if not. Only reply with lowercase letters.
This is the PDF filename:
{{ $binary.data.fileName }}
This is the PDF text content:
{{ $json.text }}
Set maxTokens and temperature as per configuration.
Connect your OpenAI API credentials.
Step 9: Merge OpenAI Result with Attachment Metadata
Add a Merge node to combine the AI response with the original file info for the next step.
Step 10: Filter Matched Attachments
Add an If node Is matched.
Check if OpenAI reported true for matching content.
If matched, proceed to upload; if not, end with This is not a matched PDF no-op node.
Step 11: Upload Matched PDFs to Google Drive
Add the Google Drive node Upload file to folder.
Set folder ID parsed from the link provided in Configure.
Use binary data from the attachment to upload.
Connect your Google Drive OAuth2 credentials.
5. Customizations ✏️
- Change the Keyword: In the Configure node, update the "Match on" value to anything specific like "invoice", "receipt", or "contract" to fit your document filtering needs.
- Adjust OpenAI Response Length: Modify "replyTokenSize" in Configure to get longer or shorter AI outputs depending on your use case.
- Set a Different Google Drive Folder: Change the upload folder URL in Configure to store files in a different location easily.
- Ignore Large PDFs: Currently, large PDFs that exceed the token limit are ignored. You can customize this by adding logic to split PDFs or notify when files are too big.
6. Troubleshooting 🔧
Problem: "OpenAI API returns errors or no response."
Cause: Token limit exceeded or invalid API key.
Solution: Check the maxTokenSize and replyTokenSize settings. Verify your OpenAI API credentials.
Problem: "Attachments not uploading to Google Drive."
Cause: Incorrect folder ID or OAuth token expired.
Solution: Confirm the folder URL in the Configure node and refresh your Google Drive OAuth2 credentials.
Problem: "Emails not triggering the workflow."
Cause: Poll settings misconfigured or missing permissions.
Solution: Ensure the Gmail Trigger node has correct polling intervals and OAuth scopes.
7. Pre-Production Checklist ✅
- Verify Gmail OAuth2 permissions and ability to read emails with attachments.
- Test OpenAI credentials and API connectivity with sample prompts.
- Confirm Google Drive folder exists and is accessible.
- Send a test email with both matching and non-matching PDFs to confirm filtering accuracy.
8. Deployment Guide
Once your workflow is fully configured and tested:
- Activate the Gmail Trigger node.
- Make sure your n8n instance is running continuously to catch emails.
- Monitor the execution log to catch any failed steps.
For production environments, consider setting up alerts on workflow failure and maintaining backup copies of uploaded documents in Google Drive for audit purposes.
9. Frequently Asked Questions (FAQs)
Q: Can I use Outlook instead of Gmail?
A: Yes, but you would need to replace the Gmail Trigger node with an Outlook Trigger node and configure its OAuth accordingly.
Q: Does this consume OpenAI API credits?
A: Yes, each PDF’s extracted text is sent to OpenAI for analysis, so usage depends on your API plan.
Q: Is my data safe during processing?
A: The workflow runs on your n8n instance, so data stays under your control. Use secure credentials and consider self-hosting for enhanced security.
Q: Can this handle high email volumes?
A: It can, but consider scaling your n8n instance and optimizing polling intervals to balance load and costs.
10. Conclusion
Congratulations! You’ve built an advanced, tailored n8n workflow that automatically filters PDF attachments in Gmail emails using OpenAI’s powerful natural language understanding. This automation saves Sarah—and you—hours every week by eliminating manual searches and uploads of important documents like payslips. Your Google Drive stays organized and your document processing becomes much more reliable.
Next, consider expanding this automation to handle other document types, integrate Slack notifications for new uploads, or automate monthly reports. With this foundation, managing email attachments has never been easier or more intelligent.
Happy automating! ⚙️