If you are working on a web application, you know how important it is to
validate user input. One of the most critical pieces of user input is the
email address. In this article, we will explore how to validate emails in
Yii2, a popular PHP framework. We will cover everything from the basics of
email validation to advanced techniques for checking email existence and
formatting.

Basics of Email Validation in Yii2

email-validation

The Yii2 framework provides several built-in validators for email validation.
The EmailValidator class is the primary validator used for validating email
addresses. The EmailValidator class checks if the input is a valid email
address by checking if it complies with the RFC 822 specification. This
specification defines the syntax for email addresses.

The EmailValidator can be used in two ways:

  • As a standalone validator
  • As part of a set of validators

When using the EmailValidator as a standalone validator, you can create an
instance of the class and call its validate() method:

use yii\validators\EmailValidator;

$email = 'example@email.com';
$validator = new EmailValidator();
if ($validator->validate($email)) {
 // email is valid
} else {
 // email is invalid
}

When using the EmailValidator as part of a set of validators, you can use it
in the rules() method of a model:

use yii\validators\EmailValidator;

public function rules()
{
 return [
 // ...
 ['email', EmailValidator::className()]
 // ...
 ];
}

Advanced Email Validation Techniques in Yii2

email-list-validation
While the EmailValidator class is sufficient for basic email validation, there
are times when you need more advanced email validation techniques. Two of the
most common advanced techniques are checking email existence and email
formatting.

Checking Email Existence

email-checking

Checking email existence involves verifying if an email address actually
exists. This technique is useful for preventing fake email accounts and
reducing the risk of spam.

To check email existence in Yii2, you can use the MailboxValidator class
provided by the mailboxlayer API. The mailboxlayer API checks if an email
address exists by connecting to the email server and sending a test message.
If the message is delivered successfully, the email address exists. If the
message is not delivered, the email address does not exist.

You can install the mailboxlayer API using Composer:

composer require mailboxlayer/mailboxlayer-php

Once you have installed the mailboxlayer API, you can use it to check email
existence:

use MailboxValidator\MailboxValidator;

$email = 'example@email.com';
$validator = new MailboxValidator('YOUR_ACCESS_KEY');
if ($validator->isValid($email)) {
 // email exists
} else {
 // email does not exist
}

Note that you will need to sign up for a mailboxlayer API key to use this
service.

Checking Email Formatting

Checking email formatting involves verifying if an email address is correctly
formatted. This technique is useful for preventing email addresses with syntax
errors from being entered into your application.

To check email formatting in Yii2, you can use the EmailFormatValidator class
provided by the mailboxlayer API. The EmailFormatValidator class checks if an
email address is correctly formatted by using regular expressions to match the
email address against a set of rules.

You can install the mailboxlayer API using Composer:

composer require mailboxlayer/mailboxlayer-php

Once you have installed the mailboxlayer API, you can use it to check email
formatting:

use MailboxValidator\EmailFormatValidator;

$email = 'example@email.com';
$validator = new EmailFormatValidator('YOUR_ACCESS_KEY');
if ($validator->isValid($email)) {
 // email is correctly formatted
} else {
 // email is not correctly formatted
}

Note that you will need to sign up for a mailboxlayer API key to use this
service.

FAQs

What is email validation?

Email validation is the process of verifying if an email address is valid.
Valid email addresses are those that comply with the RFC 822 specification.

Why is email validation important?

Email validation is important for several reasons:

  • Preventing fake email accounts
  • Reducing the risk of spam
  • Preventing email addresses with syntax errors from being entered into your application

What is the EmailValidator class in Yii2?

The EmailValidator class is the primary validator used for validating email
addresses in Yii2. It checks if the input is a valid email address by checking
if it complies with the RFC 822 specification.

What is the mailboxlayer API?

The mailboxlayer API is a service that provides email validation and
verification services. It can be used to check if an email address exists and
if it is correctly formatted.

How do I install the mailboxlayer API?

You can install the mailboxlayer API using Composer:

composer require mailboxlayer/mailboxlayer-php