daveearley/daves-email-validation-tool
Composer 安装命令:
composer require daveearley/daves-email-validation-tool
包简介
An easy to use, accurate-ish & extensible email validation library for PHP 7+
README 文档
README
An extensible email validation library for PHP 7+
The aim of this library is to offer a more detailed email validation report than simply checking if an email is the valid format, and also to make it possible to easily add custom validations.
Currently this tool checks the following:
| Validation | Description |
|---|---|
| MX records | Checks if the email's domain has valid MX records |
| Valid format | Validates e-mail addresses against the syntax in RFC 822, with the exceptions that comments and whitespace folding and dotless domain names are not supported (as it uses PHP's filter_var()). |
| Email Host | Checks if the email's host (e.g gmail.com) is reachable |
| Role/Business Email^ | Checks if the email is a role/business based email (e.g info@reddit.com). |
| Disposable email provider^ | Checks if the email is a disposable email (e.g person@10minutemail.com). |
| Free email provider^ | Checks if the email is a free email (e.g person@yahoo.com). |
| Misspelled Email ^ | Checks the email for possible typos and returns a suggested correction (e.g hi@gmaol.con -> hi@gmail.com). |
^ Data used for these checks can be found here
Installation
composer require daveearley/daves-email-validation-tool
Usage
Quick Start
// Include the composer autoloader require __DIR__ . '/vendor/autoload.php'; $validator = EmailValidation\EmailValidatorFactory::create('dave@gmoil.con'); $jsonResult = $validator->getValidationResults()->asJson(); $arrayResult = $validator->getValidationResults()->asArray(); echo $jsonResult;
Expected output:
{
"valid_format": true,
"valid_mx_records": false,
"possible_email_correction": "dave@gmail.com",
"free_email_provider": false,
"disposable_email_provider": false,
"role_or_business_email": false,
"valid_host": false
}
Adding Custom Validations
To add a custom validation simply extend the EmailValidation\Validations\Validator class and implement the getResultResponse() and getValidatorName() methods. You then register the validation using the EmailValidation\EmailValidator->registerValidator() method.
Example code
// Validations/GmailValidator.php
<?php namespace EmailValidation\Validations; class GmailValidator extends Validator { public function getValidatorName(): string { return 'is_gmail'; } public function getResultResponse(): bool { $hostName = $this->getEmailAddress()->getHostPart(); return strpos($hostName, 'gmail.com') !== false; } }
// file-where-you-are-doing-your-validation.php
<?php use EmailValidation\Validations\GmailValidator; require __DIR__ . '/vendor/autoload.php'; $validator = EmailValidation\EmailValidatorFactory::create('dave@gmail.com'); $validator->registerValidator(new GmailValidator()); echo $validator->getValidationResults()->asJson();
The expected output will be:
{
"is_gmail": true,
"valid_format": true,
"valid_mx_records": false,
"possible_email_correction": "",
"free_email_provider": true,
"disposable_email_provider": false,
"role_or_business_email": false,
"valid_host": false
}
Running in Docker
docker-compose up -d
You can then validate an email by navigating to http://localhost:8880?email=email.to.validate@example.com. The result will be JSON string as per above.
Adding a custom data source
You can create your own data provider by creating a data provider class which implements the EmailValidation\EmailDataProviderInterface.
Example Code:
<?php declare(strict_types=1); namespace EmailValidation; class CustomEmailDataProvider implements EmailDataProviderInterface { public function getEmailProviders(): array { return ['custom.com']; } public function getTopLevelDomains(): array { return ['custom']; } public function getDisposableEmailProviders(): array { return ['custom.com', 'another.com']; } public function getRoleEmailPrefixes(): array { return ['custom']; } }
FAQ
Is this validation accurate?
No, none of these tests are 100% accurate. As with any email validation there will always be false positives & negatives. The only way to guarantee an email is valid is to send an email and solicit a response. However, this library is still useful for detecting disposable emails etc., and also acts as a good first line of defence.
Can I manually update the disposable email provider data?
Yes, this project relies on this great repository for its list of disposable email providers. To fetch the latest list from that repo you can run
./scripts/update-dispsable-email-providers.php
from the command line. This will fetch the data and save it to ./src/data/disposable-email-providers.php
daveearley/daves-email-validation-tool 适用场景与选型建议
daveearley/daves-email-validation-tool 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 340.61k 次下载、GitHub Stars 达 279, 最近一次更新时间为 2017 年 07 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「email」 「validate email」 「email validation」 「email verification」 「disposable email check」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 daveearley/daves-email-validation-tool 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 daveearley/daves-email-validation-tool 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 daveearley/daves-email-validation-tool 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Runn Me! Validation and Sanitization Library
Supercharged text field validation.
Integration bundle for Aura.Input with Aura.Filter
Bureaux A Partager Edit - Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js
Zend Framework 1 Validate package
Adds request-parameter validation to the SLIM 3.x PHP framework
统计信息
- 总下载量: 340.61k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 284
- 点击次数: 29
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-07-16