PHP built-in method to verify whether a mailbox exists, php built-in verification mailbox

Source: Internet
Author: User
Tags php email mx record

PHP built-in method to verify whether a mailbox exists, php built-in verification mailbox

PHP provides many methods to verify the email address. It is commonly used to write regular expressions by yourself. However, regular expressions are much more troublesome. I have provided PHP methods for verification.

Filter_var

Filter_var is a built-in variable Filtering Method in PHP. It provides many practical filters that can be used to verify integers, floating point numbers, mailboxes, URLs, and MAC addresses.

Filter_var if false is returned, the variable cannot pass the filter, that is, it is invalid.

$email = "lastchiliarch@163.com"; var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));  $email = "asb";var_dump(filter_var($email, FILTER_VALIDATE_EMAIL)); $email = "1@a.com";var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));

Output:

string(21) "lastchiliarch@163.com"bool(false)string(7) 1@a.com

False is returned for an invalid email address format such as asb, but it is still slightly defective if the 1@a.com passes.

But the general regular also through will think 1@a.com is a legitimate mailbox, then what method can be more accurate verification?

Checkdnsrr

Checkdnsrr is actually used to query the DNS records of the specified host. We can use it to verify whether the mailbox exists.

The MX record does not exist for 1@a.com.

$email = "lastchiliarch@163.com";  var_dump(checkdnsrr(array_pop(explode("@",$email)),"MX"));  $email = "1@a.com";  var_dump(checkdnsrr(array_pop(explode("@",$email)),"MX"));

Output:

  bool(true)  bool(false)

We can see that it is perfect. The only drawback is that it is too slow. After all, it is a network request. Therefore, it is not suitable for Synchronous verification of a large number of mailboxes.

Filter_var + checkdnsrr

We can check filter_var and checkdnsrr. For most illegal emails, the filter_var will be suspended, and the rest will be reused.

Checkdnsrr further judgment.

$email_arr = array("lastchiliarch@163.com", "1@a.com");  foreach($email_arr as $email) {    if (filter_var($email) === false) {      echo "invalid email: $email \n";      continue;    }     if(checkdnsrr(array_pop(explode("@",$email)),"MX") === false) {      echo "invalid email: $email \n";      continue;    }  } 

Output:

invalid email: 1@a.com

However, it should be noted that, because we only check MX records, we can only judge that 163.com exists, but it cannot be said that the user lastchiliarch exists.

To determine the existence of a mailbox more accurately, you can only connect to the smtp server for verification.

This section describes how to verify whether the mailbox, URL, and IP address are valid in PHP:

It is mainly usedFilter_var Function.

Syntax
Filter_var (variable, filter, options)
Variable is required. Specifies the variable to be filtered.
Optional. Specifies the ID of the filter to be used.
Options specifies an array containing flag/option. Check the possible flags and options of each filter.

PHP Filters

Example #1 A filter_var () example

<?phpvar_dump(filter_var('bob@example.com', FILTER_VALIDATE_EMAIL));var_dump(filter_var('http://example.com', FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED));?>

The above routine will output:

string(15) "bob@example.com"bool(false)

The above is all the content in this article. I hope it will be helpful for you to perform php mailbox verification.

Articles you may be interested in:
  • PHP + Ajax asynchronous communication Implements user name email verification (two methods)
  • Php uses the filter to verify the ipv6 Address url of the mailbox
  • Implementation of js and php email address verification
  • Summary of php verification email addresses and IP addresses
  • Php email address Regular Expression Verification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.