Automate Gmail PDF Filtering to Google Drive with OpenAI

Automatically filter and upload specific PDF attachments from Gmail to Google Drive using an n8n workflow integrated with OpenAI. This workflow identifies PDFs like payslips, saving hours of manual sorting and ensuring accurate document management.
gmailTrigger
openAi
googleDrive
+7
Workflow Identifier: 2008
NODES in Use: gmailTrigger, set, if, code, readPDF, openAi, merge, googleDrive, noOp, stickyNote

Press CTRL+F5 if the workflow didn't load.

Learn how to Build this Workflow with AI:

Visit through Desktop for Best experience

What This Workflow Does

This workflow watches for new emails with attachments in Gmail.
It opens each attachment and checks if it is a PDF.
Then it reads the text inside the PDF and asks OpenAI if the PDF contains the keyword like “payslip”.
If OpenAI says yes, the file is uploaded to a Google Drive folder.
This saves time by only saving important PDFs automatically.


Who Should Use This Workflow

This is for people who get many emails with PDFs mixed inside.
Anyone needing to pick specific document types like payslips, invoices, or contracts.
It is useful for HR, finance, and admin roles tired of manual download and sorting.
No deep coding skill is needed since the process is mostly drag and drop in n8n.


Tools and Services Used

  • Gmail: To receive emails and attachments.
  • OpenAI API: To read PDF text and decide if the file matches keyword.
  • Google Drive: To store the selected PDF files.
  • n8n: To build and run the automation workflow.

Beginner Step-by-Step: How to Use This Workflow in n8n

Step 1: Import the Workflow

  1. Click the Download button on this page to get the workflow file.
  2. Open the n8n editor where you want to run the workflow.
  3. Use the Import from File option in n8n to load the workflow.

Step 2: Add Your Credentials and Settings

  1. Open the Gmail Trigger node and add Gmail OAuth2 credentials.
  2. Open the OpenAI node and add your OpenAI API Key.
  3. Open the Google Drive node and add Google Drive OAuth2 credentials.
  4. Go to the Configure (Set) node and enter the keyword to match, like “payslip”.
  5. In the Configure node, paste your Google Drive folder URL where files will upload.

Step 3: Test the Workflow

  1. Send an email to your Gmail with some PDF attachments to test.
  2. Run the workflow manually or wait for it to trigger.
  3. Check if the matched PDF files appear in your Google Drive folder.

Step 4: Activate for Production

  1. Enable the Gmail Trigger node to run live.
  2. Make sure your n8n is running continuously to catch new emails.
  3. Monitor logs to ensure no errors happen.

For more control and security consider self-host n8n.


Inputs, Processing, and Outputs

Inputs

  • Email arrival in Gmail with attachments.
  • PDF files included in those emails.

Processing Steps

  • Extract each attachment from the email.
  • Check if the file is PDF format.
  • Read the text inside the PDF.
  • Send text to OpenAI for keyword matching.
  • Filter the files based on OpenAI’s “true” or “false” answer.

Outputs

  • Matched PDF files uploaded to Google Drive folder.
  • Ignored non-PDFs or unmatched files are skipped silently.

Edge Cases and Failures

  • If OpenAI response errors or no reply, the workflow stops for that file.
  • PDFs larger than token limit are ignored by design.
  • Non-PDF attachments skip upload.
  • If Google Drive folder ID is wrong or credentials expire, uploads fail.

Customization Ideas

  • Change the keyword in Configure node to match other terms like “invoice”.
  • Adjust OpenAI max tokens to control response size.
  • Switch Google Drive upload folder by changing the folder link.
  • Handle large PDFs by adding PDF splitting nodes or alerts.

Sample Code to Iterate Attachments

The Code node uses this code to handle each attachment:

// 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;

Sample OpenAI Prompt Used

The prompt asks OpenAI to decide if the PDF matches the keyword like this:

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 }}

Summary of Results

✓ Emails with PDFs are checked automatically.
✓ Only PDFs with matching text get saved.
✓ Saves time by cutting manual search.
✓ Keeps Google Drive clean and organized.
✓ Works fully inside n8n with no extra tools.


Frequently Asked Questions

Yes, replace the Gmail Trigger node with an Outlook trigger and set up its OAuth.
Yes, OpenAI processes each PDF text, so usage depends on the number of PDFs and your API plan.
Check that the folder ID is correct in the Configure node and refresh Google Drive OAuth credentials.
Verify that the Gmail Trigger node has the right poll settings and OAuth permissions.

Promoted by BULDRR AI

Related Workflows

Automate Viral UGC Video Creation Using n8n + Degaus (Beginner-Friendly Guide)

Learn how to automate viral UGC video creation using n8n, AI prompts, and Degaus. This beginner-friendly guide shows how to import, configure, and run the workflow without technical complexity.
Form Trigger
Google Sheets
Gmail
+37
Free

AI SEO Blog Writer Automation Workflows in n8n

A complete beginner guide to building an AI SEO blog writer automation using n8n.
AI Agent
Google Sheets
httpRequest
+5
Free

Automate CrowdStrike Alerts with VirusTotal, Jira & Slack

This workflow automates processing of CrowdStrike detections by enriching threat data via VirusTotal, creating Jira tickets for incident tracking, and notifying teams on Slack for quick response. Save hours daily by transforming complex threat data into actionable alerts effortlessly.
scheduleTrigger
httpRequest
jira
+5
Free

Automate Telegram Invoices to Notion with AI Summaries & Reports

Save hours on financial tracking by automating invoice extraction from Telegram photos to Notion using Google Gemini AI. This workflow extracts data, records transactions, and generates detailed spending reports with charts sent on schedule via Telegram.
lmChatGoogleGemini
telegramTrigger
notion
+9
Free

Automate Email Replies with n8n and AI-Powered Summarization

Save hours managing your inbox with this n8n workflow that uses IMAP email triggers, AI summarization, and vector search to draft concise replies requiring minimal review. Automate business email processing efficiently with AI guidance and Gmail integration.
emailReadImap
vectorStoreQdrant
emailSend
+12
Free

Automate Email Campaigns Using n8n with Gmail & Google Sheets

This n8n workflow automates personalized email outreach campaigns by integrating Gmail and Google Sheets, saving hours of manual follow-up work and reducing errors in email sequences. It ensures timely follow-ups based on previous email interactions, optimizing communication efficiency.
googleSheets
gmail
code
+5
Free