├── LICENSE.md ├── README.md ├── composer.json └── src ├── Response.php ├── SmtpValidator.php ├── SmtpValidatorFacade.php └── SmtpValidatorServiceProvider.php /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 8 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 9 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 10 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 11 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 12 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 13 | THE SOFTWARE. 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Smtp Validator 2 | This laravel package can be used to validate your smtp credentials without sending a test mail 3 | 4 | # Package Installation 5 | Require this package in your composer.json file and update composer. 6 | 7 | ``` 8 | composer require samolabams/smtp-validator 9 | ``` 10 | * If you are on laravel 5.5 and above, thats all you need to do. 11 | * If your laravel version is < 5.5, you'll need to register a service provider and optionally add a facade. Open up config/app and the following to your ```providers``` array: 12 | 13 | ``` 14 | Samolabams\SmtpValidator\SmtpValidatorServceProvider::class, 15 | ``` 16 | Optionally, you can also use a facade for shorter code: 17 | 18 | ``` 19 | 'SmtpValidator' => Samolabams\SmtpValidator\SmtpValidatorFacade::class, 20 | ``` 21 | 22 | # Usage 23 | Create a new validation with the "newValidation" method with your hostname and port(default port is 25) as parameters, optionally add a username and password, encryption type and "validate" VOILA!!!. 24 | 25 | Use the App container 26 | 27 | ``` 28 | $smtpValidator = App::make('smtp-validator'); 29 | $smtpValidator->newValidation($host, $port); 30 | $smtpValidator->validate(); 31 | ``` 32 | 33 | Or use the facade 34 | ``` 35 | SmtpValidator::newValidation($host, $port)->validate(); 36 | 37 | If you have a smtp username/password and encryption, you can chain them to the validation calls 38 | 39 | SmtpValidator::newValidation($host, $port)->setUsername($username)->setPassword($password)->setEncryption($encryption)->validate(); 40 | ``` 41 | Response 42 | ``` 43 | The validate method returns a response object, with a success property (true/false value) and an optional message property which tells you the reason why your smtp credentials is invalid. 44 | 45 | $response = SmtpValidator::newValidation($host, $port)->validate(); 46 | if ($response->success === true) { 47 | // GOOD TO GO!!! 48 | } else { 49 | // SOMETHING WENT WRONG!!! 50 | $message = $response->message; 51 | } 52 | 53 | ``` 54 | # Contributing 55 | Please feel free to fork this package and contribute by submitting a pull request to enhance the functionalities. 56 | 57 | # License 58 | This Smtp Validator is licensed under the MIT License - see the [LICENSE](LICENSE.md) file for details -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "samolabams/smtp-validator", 3 | "description": "A simple laravel package that validates your smtp credentials without sending a test mail", 4 | "type": "library", 5 | "license": "MIT", 6 | "homepage": "https://github.com/samolabams/smtp-validator", 7 | "keywords": [ 8 | "samolabams", 9 | "laravel", 10 | "smtp", 11 | "laravel-smtp", 12 | "validation", 13 | "validator", 14 | "validate" 15 | ], 16 | "authors": [ 17 | { 18 | "name": "Sam Olabamiji", 19 | "email": "samolabams@gmail.com", 20 | "role": "Developer" 21 | } 22 | ], 23 | "require": { 24 | "php": ">=5.5.9", 25 | "illuminate/support": "~5.0", 26 | "swiftmailer/swiftmailer": ">=5.1" 27 | }, 28 | "autoload": { 29 | "psr-4": { 30 | "Samolabams\\SmtpValidator\\": "src/" 31 | } 32 | }, 33 | "extra": { 34 | "laravel": { 35 | "providers": [ 36 | "Samolabams\\SmtpValidator\\SmtpValidatorServiceProvider" 37 | ], 38 | "aliases": { 39 | "SmtpValidator": "Samolabams\\SmtpValidator\\SmtpValidatorFacade" 40 | } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Response.php: -------------------------------------------------------------------------------- 1 | success = $success; 20 | $this->message = $message; 21 | } 22 | 23 | public function get() 24 | { 25 | $response = new \StdClass; 26 | $response->success = $this->success; 27 | $response->message = $this->message; 28 | 29 | return $response; 30 | } 31 | } -------------------------------------------------------------------------------- /src/SmtpValidator.php: -------------------------------------------------------------------------------- 1 | host = $host; 35 | $this->port = $port; 36 | 37 | return $this; 38 | } 39 | 40 | /** 41 | * Set the username 42 | * 43 | * @param string $username 44 | * @return $this 45 | */ 46 | public function setUsername($username) 47 | { 48 | $this->username = $username; 49 | return $this; 50 | } 51 | 52 | /** 53 | * Set the password 54 | * 55 | * @param string $password 56 | * @return $this 57 | */ 58 | public function setPassword($password) 59 | { 60 | $this->password = $password; 61 | return $this; 62 | } 63 | 64 | /** 65 | * Set the encryption type 66 | * 67 | * @param string $encryption 68 | * @return $this 69 | */ 70 | public function setEncryption($encryption) 71 | { 72 | $this->encryption = $encryption; 73 | return $this; 74 | } 75 | 76 | /** 77 | * Validate SMTP credebtials 78 | * 79 | * @throws \SwiftTransportException 80 | * @throws \Exception 81 | * @return Samolabams\SmtpValidator\Response 82 | */ 83 | public function validate() 84 | { 85 | if (is_null($this->host)) { 86 | throw new Exception("Host has not been set"); 87 | } 88 | if (is_null($this->port)) { 89 | throw new Exception("Port has not been set"); 90 | } 91 | 92 | $transport = new SmtpTransport($this->host, $this->port); 93 | 94 | if ($this->encryption != null) { 95 | $transport->setEncryption($this->encryption); 96 | } 97 | 98 | if ($this->username != null && $this->password != null) { 99 | $transport->setUsername($this->username); 100 | $transport->setPassword($this->password); 101 | } 102 | 103 | $response = new Response; 104 | 105 | try { 106 | $transport->start(); 107 | $transport->stop(); 108 | $response->set(true, 'OK'); 109 | } catch(SwiftTransportException $e) { 110 | $response->set(false, $e->getMessage()); 111 | } catch(Exception $e) { 112 | $response->set(false, $e->getMessage()); 113 | } 114 | 115 | return $response->get(); 116 | } 117 | } -------------------------------------------------------------------------------- /src/SmtpValidatorFacade.php: -------------------------------------------------------------------------------- 1 | app->bind('smtp-validator', function () { 36 | return new SmtpValidator; 37 | }); 38 | 39 | $this->app->alias('smtp-validator', 'Samolabams\SmtpValidator\SmtpValidator'); 40 | } 41 | 42 | public function provides() 43 | { 44 | return ['smtp-validator']; 45 | } 46 | } 47 | --------------------------------------------------------------------------------