Introduction
Email subaddressing, also known as “plus aliasing,” is a feature supported by many email providers that allows users to create variations of their email address by appending a plus sign (+
) and additional text before the @
symbol. For example, if your email address is [email protected]
, you could use [email protected]
or [email protected]
. Emails sent to these aliases ultimately land in the same account inbox.
Benefits
This technique helps us in several ways:
-
Email Organization: By using different aliases, you can filter and categorize emails automatically. For example, emails sent to
[email protected]
could be routed to a folder dedicated to shopping-related emails. -
Spam Filtering: It’s easier to identify and block spam if you use unique aliases for different services or sign-ups.
-
Tracking Source: You can track which email addresses are being used by which services or marketing campaigns.
When to Use Email Subaddresses
-
User Sign-Ups: During user registration processes, using plus addressing can help in categorizing or filtering sign-up requests.
-
Testing: Developers often use plus addressing to test email functionality without creating multiple email accounts.
-
Organization: For personal or professional email management, it helps to keep track of different types of communications.
How to Check For Subaddresses
When dealing with email addresses in user sign-ups or other validation scenarios, you might need to validate if an email contains a plus alias.
There is a simple approach to check for plus aliases, using regex pattern matching:
- Define the Regex Pattern: The regex pattern should match a valid email structure and check for the presence of a plus sign followed by additional text:
^[^\s@]+\+[^\s@]+@
This pattern ensures that there is at least one +
sign followed by some characters in the local part of the email address before the @
symbol.
- Implement the Check: Use the regex pattern in a function to validate email addresses.
Here are two implementations - in Elixir and in Ruby:
Check for Email Subaddresses in Elixir
Here’s a simple Elixir module that checks if an email address contains a plus alias:
defmodule EmailValidator do
@moduledoc """
A module for validating an email address
"""
# Regular expression to check for plus alias in the local part of the email
@plus_alias_regex ~r/^[^\s@]+\+[^\s@]+@/
@doc """
Checks if the given email address contains a plus alias.
Returns `true` if the email contains a plus alias, otherwise returns `false`.
## Examples
iex> EmailValidator.subaddress?("[email protected]")
true
iex> EmailValidator.subaddress?("[email protected]")
false
iex> EmailValidator.subaddress?("[email protected]")
true
"""
def subaddress?(email) when is_binary(email) do
Regex.match?(@plus_alias_regex, email)
end
...
end
Check for Email Subaddress in Ruby
In Ruby, you can use the Regexp class to define and use regular expressions for validating email addresses:
class EmailValidator
PLUS_ALIAS_REGEX = /^[^\s@]+\+[^\s@]+@/
def self.subaddress?(email)
email.is_a?(String) && PLUS_ALIAS_REGEX.match?(email)
end
...
end
# Examples
> EmailValidator.subaddress?("[email protected]")
true
> EmailValidator.subaddress?("[email protected]")
false
> EmailValidator.subaddress?("user@example")
false
> EmailValidator.subaddress?("[email protected]")
false
Conclusion
Email subaddressing is a powerful feature for managing email communications and can be particularly useful in user sign-ups and other scenarios. With a simple regex check in Elixir or Ruby, you can easily validate whether an email address includes a plus alias or not. This approach provides the ability to filter out such email subaddresses in your applications, be it for identifying spam users or strictly enforcing only one user account per unique email signup.