Product
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.
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 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.
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:
Without further ado, let’s dive in.
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:
Let’s dive into some examples of these in the three sections below.
In this section, we’ll go over some variations on how you can match an email recipient’s name using regex.
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:
match_recipient('^chris@(.*)
Remember, you must have your MX records pointed to Mailgun before Mailgun will accept messages for that particular 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.”
match_recipient("^chris+(.*)@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.
Let’s find more than one specific recipient with email addresses in a specific domain. Check out the regex below.
match_recipient('^(chris|blog|test)@example.com
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.
match_recipient('^(.*)@example.com
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.
match_recipient('(?P<user>.*?)@example.com') -> forward('g<user>@externaldomain.com')
Here, we’ll go through variations on matching specific headers from an incoming email by using regular expressions.
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>.”
match_header('from', '(.*)bob@example.com(.*)')
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!”
match_header('subject', '(.*)(urgent|help|asap)(.*)')
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.
match_header('X-Mailgun-Sflag', 'Yes') -> forward('mbx@externaldomain.com')
Let’s put it all together. In this section, we’ll match based on multiple attributes.
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.
match_recipient('^(.*)@example.com
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.
match_recipient('^(.*)@example.com
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.
Send me the Mailjet Newsletter. I expressly agree to receive the newsletter and know that I can easily unsubscribe at any time.