Php method for determining whether an email address exists

Source: Internet
Author: User
Tags php email mx record
This article mainly introduces php's methods for determining whether a mailbox address exists. There are two methods for php to determine whether a mailbox address exists, if you are interested, you can refer to many methods for verifying the email address in PHP. the common method is to write regular expressions by yourself. However, regular expressions are much more troublesome. I have provided PHP methods for verification.


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.

For more information about filters, see filters. validate.

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

$ Email = ""; var_dump (filter_var ($ email, FILTER_VALIDATE_EMAIL); $ email = "asb"; var_dump (filter_var ($ email, FILTER_VALIDATE_EMAIL )); $ email = ""; var_dump (filter_var ($ email, FILTER_VALIDATE_EMAIL); output: string (21) "" bool (false) string (7) ""

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

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


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

$ Email = ""; var_dump (checkdnsrr (array_pop (explode ("@", $ email), "MX"); $ email = " "; 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 ("", ""); 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:

However, it should be noted that, because we only check MX records, we can only judge that 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.

The following is a php email address regular expression verification. The details are as follows:

<? Php header ("Content-Type: text/html; charset = UTF-8"); $ reply = ""; if (isset ($ _ POST ["email_address"]) {$ email_address = $ _ POST ["email_address"]; $ pattern = "/^ ([0-9A-Za-z \-_ \.] +) @ ([0-9a-z] + \\. [a-z] {2, 3 }(\\. [a-z] {2 })?) $/I "; if (preg_match ($ pattern, $ email_address) {$ reply =" the email address you entered is valid.

\ N "; $ user_name = preg_replace ($ pattern," $1 ", $ email_address); $ domain_name = preg_replace ($ pattern," $2 ", $ email_address ); $ reply. = "user name :". $ user_name."
\ N "; $ reply. =" domain name: ". $ domain_name ."
\ N ";} else {$ reply =" the email address you entered is invalid ";}}?>Email address verification procedureEmail Address Verification Program <? Php echo $ reply;?>

I hope you will like this article and it will help you.

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: 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.