search
Homephp教程PHP源码PHP accurately checks whether the email address exists

Background

There are many ways to verify email addresses in PHP. The more commonly used one is to write regular expressions yourself. However, regular expressions are troublesome. PHP has its own method for verification.

filter_var

filter_var is a variable filtering method built into PHP. It provides many practical filters and can be used to verify integers, floating point numbers, email addresses, URLs, MAC addresses, etc.

Specific filter reference: filters.validate

If filter_var returns false, it means that the variable cannot pass the filter, which means it is illegal.




$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"

For the illegal email format asb, false is returned, but for 1@a.com, it passes, but there are still some flaws.

However, if the general rules are passed, 1@a.com will be considered to be a legitimate email address. So is there any way to verify it more accurately?

checkdnsrr

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

For 1@a.com, the MX record definitely does not exist.

   
$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)

You 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 to use this method to verify a large number of mailboxes simultaneously.

filter_var+checkdnsrr

We can combine filter_var and checkdnsrr for verification. The vast majority of illegal mailboxes will definitely fail when filter_var is used, and the rest can be further judged using

checkdnsrr.

$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

But it should be noted that since we only check the MX record, we can only judge that 163.com exists, but it cannot explain the lastchiliarch This user exists.

If you want to more accurately determine the existence of the mailbox, you can only connect to the SMTP server to verify it.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)