Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/albertcolom/assert-email

PHP library check email (RFC 2822, DNS, Temporal Mail, Allowed Domains)
https://github.com/albertcolom/assert-email

assert check email php rfc2822 validate

Last synced: about 1 month ago
JSON representation

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

Awesome Lists containing this project

README

        

Assert email
==============
[![Build Status](https://travis-ci.org/albertcolom/assert-email.svg?branch=master)](https://travis-ci.org/albertcolom/assert-email)
[![Packagist](http://img.shields.io/packagist/v/albertcolom/assert-email.svg)](https://packagist.org/packages/albertcolom/assert-email)
[![License MIT](http://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/albertcolom/assert-email/blob/master/LICENSE)

PHP >= 5.4

PHP library to check email inspired in [webmozart/assert](https://packagist.org/packages/webmozart/assert)

## Installation

```php
composer require albertcolom/assert-email

```

## Example usage

```php
use albertcolom\Assert\AssertEmail;

class User
{
//...

public function setEmail(string $email)
{
AssertEmail::valid($email);
}
}

```

```php

$user = new User;

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

## Assertions

#### Valid
Check email valid RFC 2822
```php
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"
```

#### Temporal mail
Check temporary emails, it provides a built-in database of [2000+](https://github.com/albertcolom/assert-email/blob/master/resources/temporal-mail-domain.txt) domains
```php
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
Check DNS MX registry
```php
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"
```

#### Domains Allowed
Check if domain allowed list
```php
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]"
```