|
| 1 | +# sendgrid-validation |
| 2 | +[](https://packagist.org/packages/rayhughes/sendgrid-validation) |
| 3 | +[](https://php.net/) |
| 4 | +[](https://github.com/RayHughes/moneypot-api/blob/master/LICENSE) |
| 5 | + |
| 6 | +A PHP library to validate email addresses using the Twilio SendGrid [Email Validation API](https://docs.sendgrid.com/ui/managing-contacts/email-address-validation). |
| 7 | + |
| 8 | +## Prerequisites |
| 9 | +- Twilio SendGrid [Email Validation API Key](https://docs.sendgrid.com/ui/managing-contacts/email-address-validation#generating-your-email-validation-api-key) |
| 10 | +- PHP 7.4 or greater |
| 11 | +- [Composer](http://getcomposer.org/) |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```bash |
| 16 | +composer require rayhughes/sendgrid-validation |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +### Basic |
| 22 | +`EmailValidation()` initialized with default thresholds. |
| 23 | +```php |
| 24 | +use SendGridValidation\EmailValidation; |
| 25 | +use SendGridValidation\Service\SendGridService; |
| 26 | + |
| 27 | +$validation = new EmailValidation(new SendGridService('api-key')); |
| 28 | + |
| 29 | +print_r($validation->validate(' [email protected]')); |
| 30 | + |
| 31 | +/** Outputs |
| 32 | + SendGridValidation\Dto\EmailValidationDto |
| 33 | + ( |
| 34 | + [isValid] => true |
| 35 | + [isValidRisk] => true |
| 36 | + [isValidScore] => true |
| 37 | + [isDisposable] => false |
| 38 | + [hasSuggestion] => false |
| 39 | + [suggestion] => null |
| 40 | + ) |
| 41 | +**/ |
| 42 | +```` |
| 43 | +`EmailValidation()->validate()` returns an instance of `EmailValidationDto()` |
| 44 | + |
| 45 | +#### EmailValidationDto() |
| 46 | + |
| 47 | +- `$isValid` - Calculated `true` if validation result meets specified thresholds. |
| 48 | +- `$isValidRisk` - Calculated `true` specified risk criteria |
| 49 | +- `$isValidScore` - Calculated `true` if within minimum score threshold. |
| 50 | +- `$isDisposable` - Calculated `true` if an email is considered to be disposable. |
| 51 | +- `$hasSuggestion` - Calculated `true` if a suggestion is available. |
| 52 | +- `$suggestion` - Calculated email suggestion if available. |
| 53 | + |
| 54 | +```php |
| 55 | +class EmailValidationDto |
| 56 | +{ |
| 57 | + public bool $isValid = false; |
| 58 | + |
| 59 | + public bool $isValidRisk = false; |
| 60 | + |
| 61 | + public bool $isValidScore = false; |
| 62 | + |
| 63 | + public bool $isDisposable = false; |
| 64 | + |
| 65 | + public bool $hasSuggestion = false; |
| 66 | + |
| 67 | + public ?string $suggestion = null; |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +### Advanced |
| 72 | +`EmailValidation()` can be initialized with optional parameters to validate against developer specified thresholds. |
| 73 | + |
| 74 | +- `$allowRisky` - When `true`, considers risky emails `valid` if other conditions are met. |
| 75 | +- `$allowDisposable`- When `true`, considers disposable emails `valid` if other conditions are met. |
| 76 | +- `$checkValidScore` - When `true`, checks SendGrid `valid` emails against the minimum score threshold. |
| 77 | +- `$minSCore` - Default `0.30`. considers emails `invalid` if the minimum score threshold is not met. |
| 78 | + |
| 79 | +```php |
| 80 | +use SendGridValidation\EmailValidation; |
| 81 | +use SendGridValidation\Service\SendGridService; |
| 82 | + |
| 83 | +$validation = new EmailValidation( |
| 84 | + new SendGridService($apiKey), |
| 85 | + true, // bool $allowRisky = true, |
| 86 | + true, // bool $allowDisposable = true, |
| 87 | + true, // bool $checkValidScore = false, |
| 88 | + EmailValidation::MIN_SCORE // float $minScore = self::MIN_SCORE (0.30) |
| 89 | +); |
| 90 | + |
| 91 | +$validation->validate($email); |
| 92 | +``` |
0 commit comments