PHP Email Validator–电子邮件MX DNS记录检查

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP Email Validator–电子邮件MX DNS记录检查相关的知识,希望对你有一定的参考价值。

Validating an email address is one of the hardest feats on the web. A valid email can be marketing
gold, but an invalid email address is dead weight. Not only does it require a CPU-taxing php regular
expression ("/^[A-z0-9._-]+"."@" . "[A-z0-9][A-z0-9-]*". "(.[A-z0-9_-]+)*".".([A-z]{2,6})$/"),
the regular expression can be useless even after it's validated for format if the domain doesn't
exist.

A regular expression simply wont do -- we need to think more low-level. What does email at any
domain require? A DNS MX record. Well, PHP provides a great solution to validate that there's a MX
record for the provided email address' domain.
  1. <?php
  2. function domain_exists($email, $record = 'MX'){
  3. list($user, $domain) = explode('@', $email);
  4. return checkdnsrr($domain, $record);
  5. }
  6. if(domain_exists('[email protected]')) {
  7. echo('This MX records exists; I will accept this email as valid.');
  8. }
  9. else {
  10. echo('No MX record exists; Invalid email.');
  11. }

以上是关于PHP Email Validator–电子邮件MX DNS记录检查的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 email_validator 验证电子邮件地址

WTForms:安装“email_validator”以获得电子邮件验证支持

email_validator 挤压 tefxformfield 颤动

电子邮件地址检查

Validator EmailAddress可以验证一组电子邮件

向 Laravel 表单添加自定义验证错误