├── .gitignore ├── LICENSE ├── README.md ├── composer.json └── src └── MailgunValidator.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | /output 3 | /example.php -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Evan McMahon 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # php-mailgun-validation 2 | Validate email address with Mailgun's validation service (Syntax checks, DNS validation, MX validation) 3 | You can view Mailgun's blog post about the service here: http://blog.mailgun.com/free-email-validation-api-for-web-forms/ 4 | An API key is required to use this library, it can be obtained from mailgun's site after signup: 5 | https://mailgun.com/app/account/security 6 | 7 | ~~The service is free, however fair usage limits do apply (detailed in the post linked above)~~. 8 | Mailgun no longer offers this service for free, however this library works fine with the new paid service. 9 | 10 | Installing using Composer 11 | --------- 12 | You can install this package using composer: 13 | 14 | composer require overint/php-mailgun-validation 15 | 16 | Example Use Case 17 | --------- 18 | 19 | validate('test@example.com')); //bool(false) 26 | echo var_dump($validator->validate('someuser@gmail.com')); //bool(true) 27 | echo var_dump($validator->validate('someuser@hotmail.com')); //bool(true) 28 | echo var_dump($validator->validate('someuser@aol.com')); //bool(true) 29 | echo var_dump($validator->validate('misformed@domain')); //bool(false) 30 | echo var_dump($validator->validate('invalid@anonexistingdomain.com')); //bool(false) 31 | 32 | echo var_dump($validator->validateExtended('someuser@ymail.com')); 33 | 34 | object(stdClass)#207 (8) { 35 | ["address"]=> string(18) "someuser@ymail.com" 36 | ["did_you_mean"]=> NULL 37 | ["is_disposable_address"]=> bool(false) 38 | ["is_role_address"]=> bool(false) 39 | ["is_valid"]=> bool(true) 40 | ["mailbox_verification"]=> string(4) "true" 41 | ["parts"]=> object(stdClass)#208 (3) { 42 | ["display_name"]=> NULL 43 | ["domain"]=> string(9) "ymail.com" 44 | ["local_part"]=> string(8) "someuser" 45 | } 46 | ["reason"]=> NULL 47 | } 48 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "overint/php-mailgun-validation", 3 | "description": "Validate email address with Mailgun's validation service (Syntax checks, DNS validation, MX validation)", 4 | "type": "library", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "overint", 9 | "email": "emcmahonzx@gmail.com" 10 | } 11 | ], 12 | "minimum-stability": "stable", 13 | "require": {}, 14 | "autoload": { 15 | "psr-4": { 16 | "overint\\": "src" 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/MailgunValidator.php: -------------------------------------------------------------------------------- 1 | apiKey = $apiKey; 21 | } 22 | 23 | 24 | /** 25 | * Use curl to send the validation request to Mailgun 26 | * @param string $email 27 | * @return array 28 | */ 29 | private function queryMailgun($email) 30 | { 31 | $curl = curl_init(); 32 | 33 | curl_setopt_array($curl, array( 34 | CURLOPT_URL => self::API_ENDPOINT . "?api_key=" . $this->apiKey . "&address=" . urlencode($email) . '&mailbox_verification=true', 35 | CURLOPT_RETURNTRANSFER => true, 36 | CURLOPT_MAXREDIRS => 0, 37 | CURLOPT_TIMEOUT => 30, 38 | )); 39 | 40 | $response = curl_exec($curl); 41 | $err = curl_error($curl); 42 | 43 | curl_close($curl); 44 | 45 | if ($err) { 46 | throw new \Exception('Curl Error: ' . $err); 47 | } else { 48 | return json_decode($response); 49 | } 50 | } 51 | 52 | 53 | /** 54 | * Validate an email address and return a boolean indicating validity 55 | * @param string $email Email adddress to be validated 56 | * @return boolean 57 | */ 58 | public function validate($email) 59 | { 60 | $ret = $this->queryMailgun($email); 61 | return $ret->is_valid === true && $ret->mailbox_verification !== false; 62 | } 63 | 64 | /** 65 | * Validate an email address and return a detailed infomation from Mailgun 66 | * @param string $email Email adddress to be validated 67 | * @return array 68 | */ 69 | public function validateExtended($email) 70 | { 71 | return $this->queryMailgun($email); 72 | } 73 | 74 | 75 | } 76 | --------------------------------------------------------------------------------