Automating PDF Attachments from Gmail using MuleSoft
8/20/2025
Introduction
In this short Proof‑of‑Concept (PoC), we’ll build an email‑driven ingestion flow using Gmail and MuleSoft. The idea is simple:
1.Gmail receives emails with PDF attachments from a specific sender.
2. A Gmail filter auto‑labels those emails as Incoming (a custom label).
3. A Mule flow (On New Email – IMAP) polls the Incoming label, downloads attachments to a local folder, and then moves the processed email to Processed (or Failed if something goes wrong).
Objective
1. Create three Gmail labels: Incoming, Processed, Failed.
2. Add a Gmail rule (filter) so emails from one specific sender go straight to Incoming.
3. In MuleSoft, use On New Email (IMAP) to poll Incoming.
4. Save PDF attachments to a local input folder using the File connector.
5. Move the email to Processed if everything succeeds; move to Failed if any step fails.
STEP 1: Gmail Setup
1.1 Create labels (folders)
In Gmail (web): Settings → See all settings → Labels → Create new label.
Create these exact labels:
→ INCOMING
→ PROCESSED
→ FAILED
1.2 Enable IMAP
If there is an option to enable IMAP during this step, proceed with enabling it; otherwise, skip this step.
Gmail (web): Settings → See all settings → Forwarding and POP/IMAP → IMAP access → Enable IMAP → Save changes.
1.3 (If 2FA is ON) Create an App Password
1. Go to your Gmail account settings: https://myaccount.google.com/security
2. Enable 2-Step Verification if it's not already enabled.
3. After enabling it, generate a 16-digit App Password for use in third-party applications (as shown in the diagram below).
1.4 Create a filter to auto‑label "Incoming"
Gmail (web): Settings → See all settings → Filters and Blocked Addresses → Create a new filter.
1. In From, enter your test sender address (e.g., sender@example.com).
2. Click Create filter.
3. Select: Apply the label → choose Incoming.
4. (Optional) Check: Skip the Inbox (Archive it) to keep Inbox clean.
5. Create filter.
Now any email from that sender is auto‑labeled Incoming and will be picked by Mule.
STEP 2: configuring API in Anypoint studio
1. Create a new Mule project by selecting file>new>mule project in Anypoint Studio (e.g., gmail-imap-poc).
2. Add connectors: Email Connector and File Connector (they’re part of the standard palette on Mule 4).
2.1 The Flow — Listen, save attachments, move email
How does it work?
1. A mail arrives from the sender, Gmail rule moves it to INCOMING.
2. Mule polls INCOMING → picks up email.
3 Logs “Flow has started”.
4. Extracts attachment(s) → saves the first one locally.
5. If successful → email marked read + moved to PROCESSED.
6. If failure → email moved to FAILED.
7. Logs “Flow has ended”.
2.2 Configuration
1. Drag and drop the “On New Email — IMAP” connector from the Mule Palette onto the canvas.
2. Add a new configuration named “Connector Configuration” by clicking on the “+” button and specify the folder name as INCOMING so that the connector polls messages from this folder.
3. Configure the “On New Email — IMAP” connector with the following settings
Connection: IMAPS Connection
Host: imap.gmail.com
Port: 993
User: <gmail account (mymailgmial.com)>
Password: <my application password>
Trust Store Configuration : Insecure
4. In the General section of the connector, set the Frequency to 1 minute.
5. Add Transform Message to Handle Attachments
6. Drag and drop a Transform Message component right after the Email Listener.
7. In the first Transform Message, set the payload to extract only the attachments. In the next Transform Message, retrieve the file name as a variable and convert the attachment payload into an octet-stream format.
8. Drag and drop file write and give the path details and content
9. Next, drag and drop the Move to Folder operation and configure it with the required settings to move the email either to the Processed folder or to the Failed folder.
STEP 3 : Send test emails with PDF attachments
1. From your chosen sender email, send a few test emails to your Gmail account with dummy PDF(s) attached.
2. Verify they appear under the Incoming label (and optionally skip the Inbox).
3. Start the Mule app and test it
Testing with success scenario
Testing with failed scenario
Conclusion
This PoC showcased a simple yet effective email automation using MuleSoft and Gmail. Emails with PDF attachments were auto-labeled, processed, and sorted based on success or failure. It’s a scalable approach for automating document intake workflows with minimal manual effort.