What Is Email Subaddressing?

Understanding Email Subaddressing: Benefits, Use Cases, and Validation in Web Development

Sep 13, 2024    m. Sep 14, 2024    #email  

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:

When to Use Email Subaddresses

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:

^[^\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.

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.



Next: How Do Control Flows Work In Elixir?