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
Automate Gmail PDFs with n8n and OpenAI

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

Learn how to Build this Workflow with AI:

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.


Automate Gmail PDFs with n8n and OpenAI

Visit through Desktop to Interact with the Workflow.

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 Twist Channel Creation and Messaging with n8n

This workflow automates creating and updating a channel in Twist and sending a personalized message to specific users. It eliminates manual setup errors and saves time managing Twist communications.

Automate Ideogram Image Generation with Google Sheets & Gmail

This workflow automates graphic design image generation via Ideogram AI, storing image data in Google Sheets and Google Drive, with email alerts via Gmail. It saves designers hours by automating image creation, remixing, review, and record-keeping.

Automate IT Support with Slack and OpenAI in n8n

Streamline IT support by automating Slack message handling using n8n and OpenAI. This workflow handles Slack DMs, filters bots, queries a Confluence knowledge base, and delivers AI-generated responses, improving support efficiency and response time.

Automate Crypto Analysis with CoinMarketCap & n8n AI Agent

Discover how this unique n8n workflow leverages CoinMarketCap’s multi-agent AI to deliver precise, real-time cryptocurrency insights directly via Telegram. Manage crypto data analysis efficiently with automated multi-source API integration.

Automate Gumroad to Beehiiv Subscriber Sync with n8n

Learn how to automatically add new Gumroad sales customers as Beehiiv newsletter subscribers using n8n automation. This workflow saves time by syncing sales data to Google Sheets CRM and notifying your Telegram channel instantly.

Generate On-Brand Blog Articles Using n8n and OpenAI

This workflow automates the creation of on-brand blog articles by analyzing existing company content using n8n and OpenAI. It extracts article structures and brand voice to produce consistent draft articles, saving significant content creation time.
1:1 Free Strategy Session
Your competitors are already automating. Are you still paying for it manually?

Do you want to adopt AI Automation?

Every hour your team does repetitive work, you're burning real money.
While you wait, faster businesses are cutting costs and moving quicker.
AI and automations aren't the future anymore — they're the present.

Book a live 1-on-1 session where we show you exactly which of your daily tasks can be automated — and what it’s costing you not to.