PHP >= 5.4
PHP library to check email inspired in webmozart/assert
composer require albertcolom/assert-email
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"
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"
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"
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"
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"