Regex email forwarding

Last updated on March 9, 2021

Did you know that you can configure your alias to match a specific pattern on ImprovMX using regular expressions?

A common pattern that you can setup easily and quickly is the following:

AliasForwards to
(contact|support|hello)richard.hendricks+$1@gmail.com

In the current case, an email sent to support@piedpiper.com will be forwarded to richard.hendricks+support@gmail.com

Yes, you read that correctly, we allow for substitutions in the regular expression you are using, allowing you to set up complex structures.

You might be a REGEX professional, and seeing a pattern like the one above might raise some eyebrows. Indeed, sending an email to “aaaacontactaaaa@piedpiper.com” would match the above pattern. But in practice, we surround all the regexes entries with the start and end block (“^” and “$“). So when testing, the pattern is, in reality, ^(contact|support|hello)$.

Using substitutions

Standard REGEX rules apply, and you can use the dollar sign (“$”) as a substitution in the forwarded email. The number following the dollar sign starts at 1 for the first group matched (using parenthesis).

This allows you to write interesting REGEX patterns and use the matched value in your destination email. The above example is a case that both leverages REGEX patterns, substitutions and GMail “plus” sign, but depending on your organization and the destination mail server, you can create one alias that will handle your entire organization. If all the emails follow the same pattern firstname.lastname, then it can be configured as ([a-z]+).([a-z]+).

The only restriction that applies is the maximum length of the alias field, which is 150 chars.

Order of importance when matching an email

At ImprovMX, we consider four levels of importance when searching for an alias, which are :

  1. The exact match
  2. The “Plus sign” match (more on that at the bottom of this guide)
  3. The Regex match
  4. The Wildcard or catch-all match *

Regardless of how many aliases an email matches, only the highest priority one will be taken into consideration.

For example, take the following alias configuration for Piedpiper.com:

AliasForwards to
contactrichard.hendricks@gmail.com
(contact|support|hello)dinesh.chugtai+$1@gmail.com
*gilfoyle+wildcard@gmail.com

An email sent to contact@piedpiper.com would match the three alias configurations in this case, but ImprovMX will use the best match case, which is the exact match: “richard.hendricks@gmail.com”.

Here’s a sample of emails sent, and which alias they’d match based on the configuration above:

Wait … what about the last line? WTF?! Yep, this is what we call the “Plus sign” match

The “plus sign” match

When we receive an email that contains the “+” sign, we handle them like Gmail does, whenever possible.

This means that any email sent to us with a + sign in the left part, will be considered without it when searching for an alias.

In this case, sending an email to contact+anything@piedpiper.com is similar to sending an email to contact@piedpiper.com and will match the same result, which is “richard.hendricks@gmail.com”.

But we go a bit further.

Since the matching email is a @gmail.com address, we already know that they work well with the plus sign too, so if the matching email is a @gmail.com address, and does not already have a “+” sign, we add the one we got in the original email.

Hence, sending an email to contact+anything@piedpiper.com will result in the email being forwarded to richard.hendricks+anything@gmail.com.

The “+anything” part is automatically added by ImprovMX.

Note: This only works for @gmail.com domains, and not for G-Suite domains as it would require us to test if these domains are hosted by GMail first, slowly the email processing time.

If your email provider also accepts the plus sign, don’t hesitate to let us know. We’ll add them to the list of accepted domains for the plus sign.