├── .scrutinizer.yml ├── .travis.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── phpunit.xml.dist ├── resources └── config │ └── emailValidator.php ├── src ├── EmailValidator.php ├── EmailValidatorFacade.php ├── EmailValidatorServiceProvider.php └── helpers.php └── tests └── EmailValidatorTest.php /.scrutinizer.yml: -------------------------------------------------------------------------------- 1 | filter: 2 | excluded_paths: [tests/*] 3 | 4 | checks: 5 | php: 6 | remove_extra_empty_lines: true 7 | remove_php_closing_tag: true 8 | remove_trailing_whitespace: true 9 | fix_use_statements: 10 | remove_unused: true 11 | preserve_multiple: false 12 | preserve_blanklines: true 13 | order_alphabetically: true 14 | fix_php_opening_tag: true 15 | fix_linefeed: true 16 | fix_line_ending: true 17 | fix_identation_4spaces: true 18 | fix_doc_comments: true 19 | 20 | tools: 21 | external_code_coverage: true -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.5 5 | - 5.6 6 | - 7.0 7 | - hhvm 8 | 9 | matrix: 10 | allow_failures: 11 | - php: 7.0 12 | - php: hhvm 13 | 14 | install: travis_retry composer install --no-interaction --prefer-source 15 | 16 | script: 17 | - phpunit --coverage-text --coverage-clover=coverage.clover 18 | 19 | after_script: 20 | - wget https://scrutinizer-ci.com/ocular.phar 21 | - php ocular.phar code-coverage:upload --format=php-clover coverage.clover 22 | 23 | notifications: 24 | slack: red-creek:5lI8ybvl6YTcCNPosh4TE13h -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ### 1.0.1 4 | - Added helper functions 5 | - Bump up php version to 5.5+ 6 | 7 | ### 1.0.0 8 | - First Stable release 9 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | We accept contributions via Pull Requests on [Github](https://github.com/busayo/laravel-email-validator). 6 | 7 | 8 | ## Pull Requests 9 | 10 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). 11 | 12 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 13 | 14 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 15 | 16 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. 17 | 18 | - **Create feature branches** - Don't ask us to pull from your master branch. 19 | 20 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 21 | 22 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 23 | 24 | 25 | ## Running Tests 26 | 27 | You can run the tests with: 28 | 29 | ```bash 30 | vendor/bin/phpunit run 31 | ``` 32 | 33 | 34 | **Happy coding**! 35 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Prosper Otemuyiwa 4 | 5 | > Permission is hereby granted, free of charge, to any person obtaining a copy 6 | > of this software and associated documentation files (the "Software"), to deal 7 | > in the Software without restriction, including without limitation the rights 8 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | > copies of the Software, and to permit persons to whom the Software is 10 | > furnished to do so, subject to the following conditions: 11 | > 12 | > The above copyright notice and this permission notice shall be included in 13 | > all copies or substantial portions of the Software. 14 | > 15 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # laravel-email-validator 2 | 3 | [![Latest Stable Version](https://poser.pugx.org/unicodeveloper/laravel-email-validator/v/stable.svg)](https://packagist.org/packages/unicodeveloper/laravel-email-validator) 4 | ![](https://img.shields.io/badge/unicodeveloper-approved-brightgreen.svg) 5 | [![License](https://poser.pugx.org/unicodeveloper/laravel-email-validator/license.svg)](LICENSE.md) 6 | [![Build Status](https://img.shields.io/travis/unicodeveloper/laravel-email-validator.svg)](https://travis-ci.org/unicodeveloper/laravel-email-validator) 7 | [![Quality Score](https://img.shields.io/scrutinizer/g/unicodeveloper/laravel-email-validator.svg?style=flat-square)](https://scrutinizer-ci.com/g/unicodeveloper/laravel-email-validator) 8 | [![Total Downloads](https://img.shields.io/packagist/dt/unicodeveloper/laravel-email-validator.svg?style=flat-square)](https://packagist.org/packages/unicodeveloper/laravel-email-validator) 9 | 10 | > Laravel 5 Package to help validate and verify your email addresses. 11 | 12 | **Note:** This depends on the paid service from http://quickemailverification.com/. 13 | 14 | ## Install 15 | 16 | [PHP](https://php.net) 5.5+ or [HHVM](http://hhvm.com) 3.3+, and [Composer](https://getcomposer.org) are required. 17 | 18 | Via Composer 19 | 20 | ``` bash 21 | $ composer require unicodeveloper/laravel-email-validator 22 | ``` 23 | 24 | Another alternative is to simply add the following line to the require block of your `composer.json` file. 25 | 26 | ``` 27 | "unicodeveloper/laravel-email-validator": "1.0.*" 28 | ``` 29 | 30 | Then run `composer install` or `composer update` to download it and have the autoloader updated. 31 | 32 | Add this to your providers array in `config/app.php` 33 | 34 | ```php 35 | 36 | // Laravel 5: config/app.php 37 | 38 | 'providers' => [ 39 | ... 40 | Unicodeveloper\EmailValidator\EmailValidatorServiceProvider::class, 41 | ... 42 | ]; 43 | ``` 44 | 45 | This package also comes with a facade 46 | 47 | ```php 48 | 49 | // Laravel 5: config/app.php 50 | 51 | 'aliases' => [ 52 | ... 53 | 'EmailValidator' => Unicodeveloper\EmailValidator\EmailValidatorFacade::class, 54 | ... 55 | ] 56 | ``` 57 | 58 | Publish the config file by running: 59 | 60 | ```bash 61 | php artisan vendor:publish 62 | ``` 63 | 64 | The config file will now be located at `config/emailValidator.php`. 65 | 66 | ## Configuration 67 | 68 | This is the `emailValidator.php` file in the `config` directory. Go to [quickemailverification.com](http://quickemailverification.com/), sign up, get an api Key and insert here 69 | 70 | ```php 71 | /** 72 | * Config file that a user/developer can insert quickemailverficationservice api key 73 | */ 74 | return [ 75 | 'apiKey' => '' 76 | ]; 77 | ``` 78 | 79 | ## Usage 80 | With the Facades, all you need to do in your application is something like so: 81 | 82 | ```php 83 | 84 | print_r(EmailValidator::verify('kkkkk@example.com')->isValid()); 85 | // returns Array ( [0] => [1] => Could not get MX records for domain ) 86 | 87 | print_r(EmailValidator::verify('prosperotemuyiwa@gmail.com')->isValid()); 88 | // returns Array ( [0] => 1 [1] => SMTP server accepted email address ) 89 | 90 | var_dump( EmailValidator::verify('prosperotemuyiwa@gmail.com')->isValid()[0]); 91 | // returns bool(true) 92 | 93 | var_dump( EmailValidator::verify('kkkkk@example.com')->isValid()[0]); 94 | // returns bool(false) 95 | 96 | 97 | if( EmailValidator::verify('kkkkk@example.com')->isValid()[0] ){ 98 | ...... 99 | } 100 | 101 | // returns a true/false if the email address is valid or not 102 | ``` 103 | 104 | ### Other Methods Available 105 | ```php 106 | /** 107 | * Returns true or false if the email address uses a disposable domain 108 | * @return boolean 109 | */ 110 | EmailValidator::verify('kkkkk@example.com')->isDisposable() 111 | ``` 112 | 113 | ```php 114 | /** 115 | * Returns true or false if the API request was successful 116 | * @return boolean 117 | */ 118 | EmailValidator::verify('kkkkk@example.com')->apiRequestStatus() 119 | ``` 120 | 121 | ```php 122 | /** 123 | * Get the domain of the provided email address 124 | * @return string 125 | */ 126 | EmailValidator::verify('kkkkk@example.com')->getDomainName() 127 | ``` 128 | 129 | ```php 130 | /** 131 | * Get the local part of an email address 132 | * Example: kkkkk@example.com returns kkkkk 133 | * @return string 134 | */ 135 | EmailValidator::verify('kkkkk@example.com')->getUser() 136 | ``` 137 | 138 | ```php 139 | /** 140 | * Gets a normalized version of the email address 141 | * Example: KkkKk@example.com returns kkkkk@gmail.com 142 | * @return string 143 | */ 144 | EmailValidator::verify('kkkkk@example.com')->getEmailAddress() 145 | ``` 146 | 147 | ```php 148 | /** 149 | * Returns true if the domain appears to accept all emails delivered to that domain 150 | * @return boolean 151 | */ 152 | EmailValidator::verify('kkkkk@example.com')->acceptEmailsDeliveredToDomain() 153 | ``` 154 | 155 | ```php 156 | /** 157 | * Returns true or false if email address is a role address 158 | * Example manager@example.com , ceo@example.com will return true 159 | * @return boolean 160 | */ 161 | EmailValidator::verify('kkkkk@example.com')->isRole() 162 | ``` 163 | 164 | 165 | ## Change log 166 | 167 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 168 | 169 | ## Todo: Testing 170 | ## Todo: Verify a list of emails 171 | 172 | You can run the tests with: 173 | 174 | ```bash 175 | vendor/bin/phpunit run 176 | ``` 177 | 178 | Alternatively, you can run the tests like so: 179 | 180 | ```bash 181 | composer test 182 | ``` 183 | 184 | ## Contributing 185 | 186 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 187 | 188 | ## How can I thank you? 189 | 190 | Why not star the github repo? I'd love the attention! Why not share the link for this repository on Twitter or HackerNews? Spread the word! 191 | 192 | Don't forget to [follow me on twitter](https://twitter.com/unicodeveloper)! 193 | 194 | Thanks! 195 | Prosper Otemuyiwa. 196 | 197 | ## License 198 | 199 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 200 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "unicodeveloper/laravel-email-validator", 3 | "description": "Provides a Facade to help validate and verify email addresses", 4 | "keywords": [ 5 | "busayo", 6 | "laravel-email-validator", 7 | "laravel", 8 | "validate", 9 | "verify", 10 | "year", 11 | "email-validator", 12 | "email-verifier" 13 | ], 14 | "homepage": "https://github.com/busayo/laravel-email-validator", 15 | "license": "MIT", 16 | "authors": [ 17 | { 18 | "name": "unicodeveloper", 19 | "email": "prosperotemuyiwa@gmail.com", 20 | "homepage": "http://goodheads.io", 21 | "role": "Developer" 22 | } 23 | ], 24 | "require": { 25 | "php" : ">=5.5.9", 26 | "quickemailverification/quickemailverification": "*" 27 | }, 28 | "require-dev": { 29 | "phpunit/phpunit" : "4.*", 30 | "scrutinizer/ocular": "~1.1" 31 | }, 32 | "autoload": { 33 | "psr-4": { 34 | "Unicodeveloper\\EmailValidator\\": "src" 35 | }, 36 | "files": [ 37 | "src/helpers.php" 38 | ] 39 | }, 40 | "autoload-dev": { 41 | "psr-4": { 42 | "Unicodeveloper\\EmailValidator\\Test\\": "tests" 43 | } 44 | }, 45 | "scripts": { 46 | "test": "phpunit" 47 | }, 48 | "extra": { 49 | "branch-alias": { 50 | "dev-master": "1.0-dev" 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | tests 15 | 16 | 17 | 18 | 19 | src/ 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /resources/config/emailValidator.php: -------------------------------------------------------------------------------- 1 | '' 8 | ]; -------------------------------------------------------------------------------- /src/EmailValidator.php: -------------------------------------------------------------------------------- 1 | 'Specified email has invalid email address syntax', 17 | 'invalid_domain' => 'Domain name does not exist', 18 | 'rejected_email' => 'SMTP server rejected email. Email does not exist', 19 | 'accepted_email' => 'SMTP server accepted email address', 20 | 'no_connect' => 'SMTP server connection failure', 21 | 'timeout' => 'Session time out occurred at SMTP server', 22 | 'unavailable_smtp' => 'SMTP server is not available to process request', 23 | 'unexpected_error' => 'An unexpected error has occurred', 24 | 'no_mx_record' => 'Could not get MX records for domain', 25 | 'temporarily_blocked' => 'Email is temporarily greylisted', 26 | 'exceeded_storage' => 'SMTP server rejected email. Exceeded storage allocation' 27 | ]; 28 | 29 | /** 30 | * Api Key for quickemailverficationservice 31 | * @var string 32 | */ 33 | public $apiKey; 34 | 35 | /** 36 | * Instance of QuickEmailVerification Client 37 | * @var object 38 | */ 39 | public $client; 40 | 41 | /** 42 | * Response from the QuickEmailVerification Service 43 | * @var object 44 | */ 45 | public $response; 46 | 47 | public function __construct() 48 | { 49 | $this->setApiKey(); 50 | $this->setClient(); 51 | } 52 | 53 | /** 54 | * Get apiKey from Config file 55 | * @return void 56 | */ 57 | public function setApikey() 58 | { 59 | $this->apiKey = Config::get('emailValidator.apiKey'); 60 | } 61 | 62 | /** 63 | * Instantiate QuickEmailVerification Client with api Key 64 | * @return void 65 | */ 66 | public function setClient() 67 | { 68 | if(empty($this->apiKey)) 69 | { 70 | throw new InvalidArgumentException('Api key can not be empty'); 71 | } 72 | 73 | $this->client = new Client($this->apiKey); 74 | } 75 | 76 | /** 77 | * Verifies email Address and returns an API Object response 78 | * @param string $emailAddress 79 | * @return instance 80 | */ 81 | public function verify($emailAddress) 82 | { 83 | $this->response = $this->client->quickemailverification()->verify($emailAddress); 84 | 85 | return $this; 86 | } 87 | 88 | /** 89 | * Returns an array with true/false and an appropriate message when doing the email verification 90 | * @return array 91 | * @throws \InvalidArgumentException 92 | */ 93 | public function isValid() 94 | { 95 | $status = $this->response->body['result']; 96 | $reason = $this->response->body['reason']; 97 | 98 | switch($status): 99 | case 'valid': 100 | return [true, $this->getReason($reason)]; 101 | break; 102 | case 'invalid': 103 | case 'unknown': 104 | return [false, $this->getReason($reason)]; 105 | break; 106 | default: 107 | throw new InvalidArgumentException('Invalid Response'); 108 | endSwitch; 109 | } 110 | 111 | /** 112 | * Get the reason whether the email is valid, invalid or unknown 113 | * @param string $reason 114 | * @return string 115 | */ 116 | public function getReason($reason) 117 | { 118 | return $this->serverResponses[$reason]; 119 | } 120 | 121 | /** 122 | * Returns true or false if the email address uses a disposable domain 123 | * @return boolean 124 | */ 125 | public function isDisposable() 126 | { 127 | return $this->response->body['disposable']; 128 | } 129 | 130 | /** 131 | * Returns true or false if the API request was successful 132 | * @return boolean 133 | */ 134 | public function apiRequestStatus() 135 | { 136 | return $this->response->body['success']; 137 | } 138 | 139 | /** 140 | * Get the domain of the provided email address 141 | * @return string 142 | */ 143 | public function getDomainName() 144 | { 145 | return $this->response->body['domain']; 146 | } 147 | 148 | /** 149 | * Get the local part of an email address 150 | * Example: prosperotemuyiwa@gmail.com returns prosperotemuyiwa 151 | * @return string 152 | */ 153 | public function getUser() 154 | { 155 | return $this->response->body['user']; 156 | } 157 | 158 | /** 159 | * Gets a normalized version of the email address 160 | * Example: ProsperOtemuyiwa@gmail.com returns prosperotemuyiwa@gmail.com 161 | * @return string 162 | */ 163 | public function getEmailAddress() 164 | { 165 | return $this->response->body['email']; 166 | } 167 | 168 | /** 169 | * Returns true if the domain appears to accept all emails delivered to that domain 170 | * @return boolean 171 | */ 172 | public function acceptEmailsDeliveredToDomain() 173 | { 174 | return $this->response->body['accept_all']; 175 | } 176 | 177 | /** 178 | * Returns true or false if email address is a role address 179 | * Example manager@example.com , ceo@example.com will return true 180 | * @return boolean 181 | */ 182 | public function isRole() 183 | { 184 | return $this->response->body['role']; 185 | } 186 | } 187 | -------------------------------------------------------------------------------- /src/EmailValidatorFacade.php: -------------------------------------------------------------------------------- 1 | publishes([ 27 | $config => config_path('emailValidator.php') 28 | ]); 29 | } 30 | 31 | /** 32 | * Register the application services 33 | * @return void 34 | */ 35 | public function register() 36 | { 37 | $this->app->bind('laravel-email-validator', function(){ 38 | 39 | return new EmailValidator; 40 | 41 | }); 42 | } 43 | 44 | /** 45 | * Get the services provided by the provider 46 | * @return array 47 | */ 48 | public function provides() 49 | { 50 | return ['laravel-email-validator']; 51 | } 52 | } -------------------------------------------------------------------------------- /src/helpers.php: -------------------------------------------------------------------------------- 1 | assertTrue(true); 12 | } 13 | } 14 | --------------------------------------------------------------------------------