PHP code example of albertcolom / assert-email

1. Go to this page and download the library: Download albertcolom/assert-email library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

albertcolom / assert-email example snippets


composer 

use albertcolom\Assert\AssertEmail;

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



$user = new User;

$user->setEmail('[email protected]'); // true
$user->setEmail('foo@domain'); // InvalidArgumentException: Invalid email "foo@domain"

valid($email, $message = '')

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

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

AssertEmail::temporalMail('[email protected]'); // true
AssertEmail::temporalMail('[email protected]'); // InvalidArgumentException: Temporal email is not allowed "[email protected]"
AssertEmail::temporalMail('[email protected]', 'Custom message %s'); // InvalidArgumentException: Custom message "[email protected]"

dns($email, $message = '')

AssertEmail::dns('[email protected]'); // true
AssertEmail::dns('[email protected]'); // InvalidArgumentException: Incorrect domain name "domain.000"
AssertEmail::dns('[email protected]', 'Custom message %s'); // InvalidArgumentException: Custom message "domain.000"

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

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

AssertEmail::domainsAllowed('[email protected]', $allowed); // true
AssertEmail::domainsAllowed('[email protected]', $allowed); // InvalidArgumentException: Domain is not allowed "[email protected]"
AssertEmail::domainsAllowed('[email protected]', $allowed, 'Custom message %s'); // InvalidArgumentException: Custom message "[email protected]"