About this workflow
Unsubscribe Mautic Contacts from Automated Unsubscribe Emails – Workflow Analysis
This N8n workflow template is designed to automatically manage email unsubscribe requests received via Gmail by integrating with Mautic, a popular open-source marketing automation platform. When someone sends an email with "unsubscribe" in the subject or body (or related fields), this workflow identifies the sender, verifies their contact status in Mautic, and automatically processes their opt-out request by updating relevant segments, optionally adding them to a do-not-contact list, and sending back a confirmation reply.
The entire process runs on event-triggered automation within n8n, eliminating manual intervention and ensuring compliance with email marketing best practices such as timely opt-out handling.
What the Workflow Does
This workflow listens for incoming emails through Gmail, filters those that appear to be unsubscribe requests, extracts the sender’s email address, checks if they exist in your Mautic CRM, and then performs several automated actions:
- Adds the contact to an “Unsubscribed” segment
- Removes them from active marketing segments (e.g., newsletter)
- Optionally adds them to Mautic's Do Not Contact list
- Sends an automatic reply confirming the unsubscription
It ensures prompt, accurate, and scalable management of unsubscribe requests without requiring user input after setup.
Key Features and Capabilities
- ✅ Real-time Email Monitoring: Uses Gmail Trigger node to poll new messages every minute, including spam/trash folders.
- ✅ Smart Filtering: Identifies legitimate unsubscribe requests using conditional logic based on recipient ("To") field content.
- ✅ Email Parsing Logic: Handles complex "From" field formats (e.g.,
John Doe <[email protected]>) using custom JavaScript code. - ✅ Duplicate Prevention: Aggregates unique email addresses before querying Mautic, improving efficiency and avoiding redundant API calls.
- ✅ CRM Integration: Seamlessly interacts with Mautic to update contact segmentation and communication preferences.
- ✅ Automated Response: Sends a personalized auto-reply acknowledging successful opt-out.
- ✅ Configurable Settings: Centralized configuration via the Edit Fields node allows easy customization of email address and response message.
- ✅ Scalable & Reliable: Processes multiple unsubscribe requests at once while maintaining data integrity.
Main Nodes and Their Purposes
| Node Name | Type | Purpose |
|---|---|---|
| Gmail Trigger | Trigger | Listens for new incoming emails in real time; polls every minute and includes spam/trash to catch all possible unsubscribe messages. |
| Edit Fields | Set | Stores configurable values like the user’s own email address and the unsubscribe reply message. Acts as a central config point. |
| Is automated unsubscribe? | If Condition | Filters emails where the "To" field contains "unsubscribe" AND the sender isn't the same as the configured email (to avoid self-triggering). |
| Extract Email from 'From' Field | Code (JavaScript) | Parses raw "From" header to extract clean email address using regex, supporting both [email protected] and "Name <[email protected]>" formats. |
| Extract Unique Email Addresses | Code (JavaScript) | Deduplicates extracted emails using a Set, preventing redundant operations when multiple emails come from the same sender. |
| Get Mautic Contact ID from Email Address | Mautic | Queries Mautic API to retrieve contact details using the extracted email address (search=email:...). Returns only one result (limit=1). |
| If Contact Exists in Mautic | If Condition | Checks whether the queried contact exists in Mautic (i.e., has an id). Determines next steps accordingly. |
| Add to unsubscribed segment | Mautic | Adds the identified contact to a predefined Mautic segment (ID: 3) indicating they have unsubscribed. |
| Remove newsletter segment | Mautic | Removes the contact from another segment (ID: 1), likely used for newsletters or campaigns. |
| Add to Do Not Contact List | Mautic (disabled) | Would mark the contact as “Do Not Contact” in Mautic; currently disabled but can be enabled for stricter compliance. |
| Reply Unsubscribe Message | Gmail | Replies directly to the original email thread with a customizable success message stored in Edit Fields. |
| Sticky Notes | Documentation | Provide step-by-step setup instructions and context for users configuring the workflow. |
Use Cases and Benefits
Ideal For:
- Marketing teams managing large subscriber lists via Mautic
- Businesses required to comply with GDPR, CAN-SPAM, or other privacy regulations
- Agencies automating client communications and reducing manual tasks
- Any organization seeking to improve email engagement and reduce bounce/complaint rates
Key Benefits:
- ⏱️ Saves Time: Eliminates need to manually process each unsubscribe request.
- 🛡️ Improves Compliance: Ensures immediate action on opt-outs, reducing legal risk.
- 🔁 Reduces Human Error: Automation prevents missed or duplicated entries.
- 💬 Enhances User Experience: Sends instant acknowledgment to users who unsubscribe.
- 📈 Maintains List Hygiene: Keeps Mautic segments clean and up-to-date automatically.
Step-by-Step Workflow Logic
Here’s how the workflow executes sequentially:
-
Trigger: Incoming Email Detected
- The Gmail Trigger node activates whenever a new email arrives (checked every minute).
- It captures full email metadata including
From,To,Subject, andid.
-
Configuration Setup
- The Edit Fields node defines two key variables:
emailAddress: Your business email (e.g., [email protected])unsubscribeMessage: Auto-reply text confirming unsubscription
- The Edit Fields node defines two key variables:
-
Filter Valid Unsubscribe Requests
- The Is automated unsubscribe? condition checks:
- Is "unsubscribe" present in the
Tofield? → Likely an opt-out request - Is the sender NOT the same as your configured email? → Prevents loopback/self-triggering
- Is "unsubscribe" present in the
- Only emails passing both conditions proceed.
- The Is automated unsubscribe? condition checks:
-
Parse Sender’s Email Address
- The Extract Email from 'From' Field node uses JavaScript to extract the actual email address from various formats:
"[email protected]" → [email protected] "Alice Smith <[email protected]>" → [email protected] - Output:
{ "extractedEmail": "[email protected]" }
- The Extract Email from 'From' Field node uses JavaScript to extract the actual email address from various formats:
-
Deduplicate Email Addresses
- The Extract Unique Email Addresses node collects all parsed emails and removes duplicates using a JavaScript
Set. - This avoids unnecessary repeated API calls to Mautic for the same person.
- The Extract Unique Email Addresses node collects all parsed emails and removes duplicates using a JavaScript
-
Find Contact in Mautic
- The Get Mautic Contact ID from Email Address node queries Mautic:
search=email:[email protected] - Returns contact info including
idif found; otherwise returns empty or minimal data.
- The Get Mautic Contact ID from Email Address node queries Mautic:
-
Check if Contact Exists
- The If Contact Exists in Mautic condition evaluates:
- Does
$json["id"]exist and not be empty?
- Does
- If yes → Proceed to update Mautic records
- If no → Skip to sending reply anyway (acknowledge even if not in system)
- The If Contact Exists in Mautic condition evaluates:
-
Update Mautic Segments
- Three parallel Mautic actions occur:
- ✅ Add to unsubscribed segment (Segment ID: 3)
- ✅ Remove from newsletter segment (Segment ID: 1)
- ⚠️ Add to Do Not Contact List — Currently disabled, but available for activation
- Three parallel Mautic actions occur:
-
Send Confirmation Reply
- The Reply Unsubscribe Message node replies directly to the original email thread using the Gmail API.
- Message body comes from
$node["Edit Fields"].json["unsubscribeMessage"] - Maintains conversation history and provides closure to the sender.
-
Completion
- The workflow ends gracefully regardless of whether the contact existed in Mautic.
- All valid unsubscribe attempts receive a response and are processed systematically.
Summary
This n8n workflow delivers a powerful, low-code solution for managing unsubscribe requests across Gmail and Mautic. By combining triggers, conditions, code logic, and integrations, it enables businesses to maintain compliant, professional, and efficient email marketing operations — all with minimal ongoing effort. With clear documentation via sticky notes and modular design, it's also highly adaptable to different use cases and organizational needs.
How to use: download the JSON, then in n8n choose “Import from File”.