enricodias/email-validator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

enricodias/email-validator

Composer 安装命令:

composer require enricodias/email-validator

包简介

Validate and check for disposable/temporary/throw away emails using multiple providers

README 文档

README

Build Status Codacy Badge Codacy Badge Latest version Downloads total License

Validate and check for disposable/temporary/throw away emails.

Installation

Require this package with Composer in the root directory of your project.

composer require enricodias/email-validator

Basic Usage

$emailValidator = new \enricodias\EmailValidator\EmailValidator();
$emailValidator->validate('test+mail@gmail.co');

$emailValidator->isValid();      // false, gmail.co doesn't have valid MX entries
$emailValidator->isDisposable(); // false, gmail.co isn't a known domain for disposable emails
$emailValidator->isAlias();      // true, test+mail@gmail.co is alias for test@gmail.co
$emailValidator->didYouMean();   // test+mail@gmail.com

Service Providers

A service provider is a third party service that validates the email, usually using an API. You may register several providers to be used on the validation.

The registered providers will be used in sequence until one of them returns a valid response. This is especially useful if you want a provider to act as a failover.

UserCheck is enabled by default.

$MailboxLayer = new \enricodias\EmailValidator\ServiceProviders\MailboxLayer('API_KEY');

$Mailgun = new \enricodias\EmailValidator\ServiceProviders\Mailgun('API_KEY');

$emailValidator->addProvider($MailboxLayer, 'MailboxLayer');
$emailValidator->addProvider($Mailgun); // the name is optional

$emailValidator->validate('test@email.com');

Implemented providers

Provider Free Tier Cost per validation Unsupported Features
UserCheck 1000 verifications per month $0.00014 to $0.00025 isHighRisk()
MailboxLayer 250 verifications per month $0.002 to $0.0006
NeverBounce 1000 verifications $0.008 to $0.003 isHighRisk()
Kickbox 100 verifications $0.010 to $0.004
Mailgun 0 $0.012 to $0.0025 didYouMean()**

* MailCheck.ai and Validator.pizza is now called UserCheck ** the feature is documented but as for now, the API never returns a suggestion.

Custom providers

You can add a custom provider by implementing the class ServiceProviderInterface. It's possible to remove the default UserCheck provider using removeProvider() method or remove all all providers using clearProviders() method:

$emailValidator = new \enricodias\EmailValidator\EmailValidator();

$emailValidator->clearProviders(); // remove all providers

$emailValidator->addProvider($CustomServiceProvider, 'My Custom Provider');

$emailValidator->validate('test@email.com');

You can use the static method create() to create an instance and chain methods:

$emailValidator = \enricodias\EmailValidator\EmailValidator::create()
    ->removeProvider('UserCheck');
    ->addProvider($CustomServiceProvider)
    ->validate('test@email.com');

Note that providers registered without a name cannot be removed by removeProvider().

Shuffle providers

Shuffling the service providers list is useful when using the free tier of multiple providers. Without shuffling, the providers will always be used in the same order resulting in unnecessary failures when the first provider runs out of credits.

$emailValidator->clearProviders()
    ->addProvider($Provider1)
    ->addProvider($Provider2)
    ->shuffleProviders()
    ->validate('test@email.com');

How it works

The class checks locally if the email syntax is valid and if so, it calls a service provider.

Rate limit

Since most service providers are either paid or have a limit of requests per hour per ip, no request is made if the email doesn't pass on the local validation checks.

Local domain list

To lower the number of API requests the local checks include a list with the most common disposable domains. This list is intended to be short in order to not affect performance and avoid the need of constants updates. Wildcards * are allowed.

Additional Domains

It's likely that the most popular disposable email services among your users are not on the default list, so you may want to customize the list using the addDomains() method:

$emailValidator = \enricodias\EmailValidator\EmailValidator::create()
    ->addDomains(['*.domain.com'])
    ->validate('test@sub.domain.com',);

$emailValidator->isDisposable(); // true

This method doesn't accepts a string, only an array.

Validation methods

isValid()

Returns true if the email is valid.

The email is considered invalid if it fails on the local syntax check OR if it fails in the service provider's check. Note that disposable emails are valid emails.

isDisposable()

Returns true if the email is a disposable email.

isAlias()

Returns true if the email is an alias. Example: test+mail@gmail.com is an alias of test@gmail.com.

didYouMean()

If the email has a simple and obvious typo such as gmail.cm instead of gmail.com this method will return a string with a suggested correction, otherwise it will return an empty string.

It's recommended to use this feature using javascript in the client side with an option for them to correct the email before submitting the form

isHighRisk()

Most service providers have a risk analysis tool. This method returns true if the risk is high.

enricodias/email-validator 适用场景与选型建议

enricodias/email-validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 701 次下载、GitHub Stars 达 6, 最近一次更新时间为 2019 年 10 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「email」 「disposable」 「temporary email」 「email validator」 「throw away email」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 enricodias/email-validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 enricodias/email-validator 我们能提供哪些服务?
定制开发 / 二次开发

基于 enricodias/email-validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 701
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-10-20