Product
Using regular expressions to filter incoming emails
Although you’re probably already familiar with regular expressions in your app development, read on to learn about using them with Mailgun to filter incoming emails. Also, take a look at some syntax and common use cases for using regular expressions with our Routes feature.
PUBLISHED ON
Regular expressions are a powerful way to filter through and match data, but the syntax changes depending on your scripting language and use case. For instance, a regular expression following a Javascript spec differs from one following Java or Python. These same hiccups apply to using regular expressions for emails.
Although you’re probably already familiar with regular expressions in your app development, take a crash course on using them with Mailgun to filter incoming emails. We’ll also review some syntax and common use cases for using regular expressions with our Routes feature.
Table of contents
How can I match a recipient, regardless of their email address’s domain name?
How can I match a specific recipient whose email address is a specific domain?
How can I match several recipients at a specific domain?
How can I match any recipient at a specific domain?
How can I use a named capture to forward a message to an external recipient?
How can I match a defined “from” attribute?
How can I match several defined keywords in the subject line?
How can I collect and redirect incoming spam messages to an external mailbox?
How can I match any recipient whose message is a reply?
How can I match any recipient – but only if the message is in English?
Table of contents
01What are regular expressions?
02How do I use regex with Mailgun?
03What’s the regex flavor for Mailgun?
04What are some examples of using regex with Mailgun?
051. How do I match an email recipient’s name using regex?
062. How can I match specific headers from an email with regex?
What are regular expressions?
Just to cover our bases, here’s a quick refresher on regular expressions (regex or regexp). Regex is a sequence of characters that specifies a search pattern in text. You can use regex with string-searching algorithms to match in a database, text, or HTML code. Advanced use cases include greedy (find all matches) or non-greedy (find the first match and stop) variations.
Email regex is useful for filtering invalid email addresses during email address validation, catching mistakes, and filtering incoming mail.
How do I use regex with Mailgun?
Email marketing is a conversation, and Mailgun is in the business of sending emails. But wait, if it’s supposed to be a conversation, don’t you also need a way to handle incoming mail?
Glad you asked! Our Routes API lets 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. However, none of this matters if you can’t effectively filter incoming emails.
Below, let’s go through some examples of the most common ways to group incoming emails for processing. These methods use regular expressions that are extremely powerful but can also be tricky to work.
What’s the regex flavor for Mailgun?
The regex notation for Routes is based on the Python spec. Before we dive into specific examples, we want to point out a common error for developers who are new to regular expressions:
The asterisk (*) itself isn’t 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 (.*).
Without further ado, let’s dive in.
What are some examples of using regex with Mailgun?
Here are some of the most common regular expressions that we see customers use when receiving messages. This list only scratches the surface of what you can do, but hopefully, it’ll get you thinking about the power of regular expressions.
To make things easier, we’ve categorized the expressions into three groups:
Match on variations of the email recipient.
Match on email headers.
Chain regular expressions to match multiple attributes.
Let’s dive into some examples of these in the three sections below.
1. How do I match an email recipient’s name using regex?
In this section, we’ll go over some variations on how you can match an email recipient’s name using regex.
How can I match a recipient, regardless of their email address’s domain name?
Let’s start with a not-so-constrained example. In this simple regex, we want to find a specific recipient in any of the domain names currently loaded in the Mailgun Domains tab. Check out the sample code below:
Remember, you must have your MX records pointed to Mailgun before Mailgun will accept messages for that particular domain.
How can I match a specific recipient whose email address is a specific domain?
Next, we’ll step it up a bit. We still want to find a specific recipient. However, this time we want to find a specific recipient at a specific domain, “Gmail.com.”
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 next example.
How can I match several recipients at a specific domain?
Let’s find more than one specific recipient with email addresses in a specific domain. Check out the regex below.
How can I match any recipient at a specific domain?
In this case, we want to find all email recipients with valid email addresses at a specific top-level domain. This is sort of a “catch-all” for a particular domain. As a word of warning, this shouldn’t be confused with the global catch-all that Mailgun Routes provides where all emails received are forwarded.
How can I use a named capture to forward a message to an external recipient?
In this use case, we want Mailgun to receive and forward an incoming message to an external domain. However, we also want to retain the user in our user mapping.
To do this, we use a named capture, which will remember the “user” and use it in the forward action. Check out the regex below.
2. How can I match specific headers from an email with regex?
Here, we’ll go through variations on matching specific headers from an incoming email by using regular expressions.
How can I match a defined “from” attribute?
Let’s start with something simple. We want the Route to trigger for any email that’s from “bob@example.com.” Notice we add 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>.”
How can I match several defined keywords in the subject line?
If email is about conversations, timely responses to urgent queries are a great way to keep your users happy. In this case, we’re looking for any messages with a subject that contains either “urgent” OR “help” OR “asap.” We will match these strings in both uppercase and lowercase.
The wildcards in both the beginning and the end catch subject lines that contain our keywords regardless of the other text in the subject. For instance, this example would trigger a subject that says, “My request is urgent!”
How can I collect and redirect incoming spam messages to an external mailbox?
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.
Here, we’re forwarding the message to an external mailbox so that we can review it later. Check out the regex below.
3. How can I chain regex to match multiple attributes?
Let’s put it all together. In this section, we’ll match based on multiple attributes.
How can I match any recipient whose message is a reply?
In the example below, we want to match any recipient whose email address is a specific domain. However, we only want to route messages when it’s a reply to our original thread. You could also use “Fw” to represent a forwarded message.
How can I match any recipient – but only if the message is in English?
In the regex below, we’ve created a “catch-all” for all recipients from a specific domain. However, we only route messages when the content language is in English. Learn how to do this based on the ISO specification for content languages.
More from Mailgun
Our Routes feature powers your sending, but also helps you manage your incoming email conversations. Did this tutorial help you write more effective regular expressions for filtering your incoming emails? Subscribe to our newsletter so you don’t miss out on more useful content like this.