- Email DIY
Become an Email Pro With Our Templates API
One of the features that gets the most love from our customers are Routes. Routes let you accept, parse and POST or forward your incoming emails. We’ve done some work lately to make Routes even more useful by adding the ability to test your webhook endpoints when you want to POST your incoming emails to your app. None of this matters though if you are not able to effectively accept the incoming emails.
Today, we’ll walk you through some examples of the most common ways to group incoming emails for processing. These methods use Regular Expressions which are extremely powerful, but can also be tricky to work with sometimes. Hence today’s post. Hope you enjoy it!
The Regular Expression notation for Routes is based on the Python spec and there is an excellent review of Regular Expressions over at python.org. We’re not going to go into all the details, but we do want to point out one error that developers who are new to regular expressions often make:
The asterisk “*” itself is not a wildcard in regular expression syntax.
To match any character, you need to use a period “.”
To match any series of characters, you’d use a period followed by an asterisk “.*”
Here are some of the most common regular expressions that we see customers using when receiving messages. This list only scratches the surface of what you can do, but hopefully it will get you thinking about the power of regular expressions.
To make things easier, we’ve grouped the expressions into 3 groups
I. Matching on variations of the recipient of the email
II. Matching on email headers (e.g. from, subject)
III. Chaining regular expression to match on multiple attributes
match_recipient('^chris@(.*)
Explanation – we are looking for a specific recipient for any domains currently loaded in the Mailgun “Domains” tab. Keep in mind, you must have your MX records pointed to Mailgun before Mailgun will accept messages for that domain.
match_recipient("^chris+(.*)@example.com$")
Explanation – Mailgun configures our inbound mail server to accept recipients with plus addressing. You could also limit the plus addresses by using the syntax from the below example.
match_recipient('^(chris|blog|test)@example.com
Explanation – we’ve used regular expressions to define a set of recipients that are valid for a specific domain.
match_recipient('^(.*)@example.com
Explanation – we’ve created a “catch all” for a specific domain. Don’t get confused with the global catch all that Mailgun Routes provides where all emails received are forwarded.
match_recipient('(?P<user>.*?)@example.com') -> forward('g<user>@externaldomain.com')
Explanation – we want Mailgun to receive and forward the incoming message to an external domain, but retain the user to user mapping. To do this, we use a named capture. The named capture will remember the “user” and use it in the forward action.
match_header('from', '(.*)bob@example.com(.*)')
Explanation – we want the route to trigger for any email that is from “bob@example.com”. Notice we add the wildcards before and after the email address. This is because a “From” field can contain several other attributes. For example, the sender’s name. “Mailgun Bob <bob@example.com>”
match_header('subject', '(.*)(urgent|help|asap)(.*)')
Explanation – we are looking for any messages with a subject that contains “urgent” OR “help” OR “asap”. We add wildcards on both the beginning and end. This example would trigger on a subject like this: “My request is urgent!”.
match_header('X-Mailgun-Sflag', 'Yes') -> forward('mbx@externaldomain.com')
Explanation – Mailgun provides spam filtering for inbound messages. When we determine a message is spam, we inject a special header. You can use Routes to filter messages based on these headers. In this case, we are forwarding the message to an external mailbox, so we can review it later.
match_recipient('^(.*)@example.com
Explanation – we’ve created a “catch all” for a specific domain, but we only route messages when it’s a reply to our original thread. You could also use “Fw” to represent a forwarded message.
match_recipient('^(.*)@example.com
Explanation – we’ve created a “catch all” for a specific domain, but we only route messages when the content language is in English. For other languages, check out the ISO specification for content languages, https://en.wikipedia.org/wiki/ListofISO639-1codes.
We hope these examples will help you write more effective regular expressions for filtering your incoming emails. Something else you’d like to know how to write? Let us know in the comments and we’ll see if we can come up with an example.
Happy emailing!
The Mailgunners
Learn about our Deliverability Services
Looking to send a high volume of emails? Our email experts can supercharge your email performance. See how we've helped companies like Lyft, Shopify, Github increase their email delivery rates to an average of 97%.
Last updated on May 17, 2021
Become an Email Pro With Our Templates API
How to Conduct a Comprehensive Email Deliverability Audit
Dark Mode for Email Survey: What Do Email Senders Think?
Email Accessibility Mistakes that Annoy Subscribers & How To Fix Them
What’s Cool About COIL
The Difference Between SMTP and API
The Basics of Email Dark Mode
The Top Email Clients and Email Apps of 2021
The Benefits of Email Automation
Sunset Policies: Allowing Unengaged Recipients to Ride Off into the Sunset
InboxReady x Salesforce: The Key to a Stronger Email Deliverability
Become an Email Pro With Our Templates API
Google Postmaster Tools: Understanding Sender Reputation
Navigating Your Career as a Woman in Tech
Implementing Dmarc – A Step-by-Step Guide
Email Bounces: What To Do About Them
Announcing InboxReady: The deliverability suite you need to hit the inbox
Black History Month in Tech: 7 Visionaries Who Shaped The Future
How To Create a Successful Triggered Email Program
Designing HTML Email Templates For Transactional Emails
InboxReady x Salesforce: The Key to a Stronger Email Deliverability
Implementing Dmarc – A Step-by-Step Guide
Announcing InboxReady: The deliverability suite you need to hit the inbox
Designing HTML Email Templates For Transactional Emails
Email Security Best Practices: How To Keep Your Email Program Safe
Mailgun’s Active Defense Against Log4j
Email Blasts: The Dos And Many Don’ts Of Mass Email Sending
Email's Best of 2021
5 Ideas For Better Developer-Designer Collaboration
Mailgun Joins Sinch: The Future of Customer Communications Is Here
Always be in the know and grab free email resources!
By sending this form, I agree that Mailgun may contact me and process my data in accordance with its Privacy Policy.