albertcolom/assert-email

PHP library check email (RFC 2822, DNS, Temporal Mail, Allowed Domains)

1.0 2017-12-04 12:01 UTC

This package is not auto-updated.

Last update: 2024-04-21 06:58:54 UTC


README

Build Status Packagist License MIT

PHP >= 5.4

PHP library to check email inspired in webmozart/assert

Installation

composer require albertcolom/assert-email

Example usage

use albertcolom\Assert\AssertEmail;

class User
{
    //...
    
    public function setEmail(string $email)
    {
        AssertEmail::valid($email);
    }
}
$user = new User;

$user->setEmail('foo@domain.com'); // true
$user->setEmail('foo@domain'); // InvalidArgumentException: Invalid email "foo@domain"

Assertions

Valid

Check email valid RFC 2822

valid($email, $message = '')

AssertEmail::valid('foo@domain.com'); // true
AssertEmail::valid('foo@domain'); // InvalidArgumentException: Invalid email "foo@domain"
AssertEmail::valid('foo@domain', 'Custom message %s'); // InvalidArgumentException: Custom message "foo@domain"

Temporal mail

Check temporary emails, it provides a built-in database of 2000+ domains

temporalMail($email, $message = '')`

AssertEmail::temporalMail('foo@domain.com'); // true
AssertEmail::temporalMail('foo@yopmail.com'); // InvalidArgumentException: Temporal email is not allowed "foo@yopmail.com"
AssertEmail::temporalMail('foo@yopmail.com', 'Custom message %s'); // InvalidArgumentException: Custom message "foo@yopmail.com"

DNS

Check DNS MX registry

dns($email, $message = '')

AssertEmail::dns('foo@domain.com'); // true
AssertEmail::dns('foo@domain.000'); // InvalidArgumentException: Incorrect domain name "domain.000"
AssertEmail::dns('foo@domain.000', 'Custom message %s'); // InvalidArgumentException: Custom message "domain.000"

Domains Allowed

Check if domain allowed list

domainsAllowed($email, array $domains, $message = '')

$allowed = ['mysite.com', 'somedomain.xy', 'test.dev'];

AssertEmail::domainsAllowed('foo@test.dev', $allowed); // true
AssertEmail::domainsAllowed('foo@gmail.com', $allowed); // InvalidArgumentException: Domain is not allowed "foo@gmail.com"
AssertEmail::domainsAllowed('foo@gmail.com', $allowed, 'Custom message %s'); // InvalidArgumentException: Custom message "foo@gmail.com"