Get the App
SLTechnology News&Howtos  ›  Development  › 

Use PHP native method to verify the existence of mailbox

Shulou Source: shulou.com Published: 2022-06-03 19:47:18 10月16日 Update

This article mainly introduces "using the PHP built-in method to verify the existence of the mailbox". In the daily operation, I believe that many people have doubts in using the PHP built-in method to verify the existence of the mailbox. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "using the PHP built-in method to verify the existence of the mailbox". Next, please follow the editor to study!

Filter_var

Filter_var is a variable filtering method built into PHP, which provides many practical filters that can be used to check integers, floating-point numbers, mailboxes, URL, MAC addresses, and so on.

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

The illegal mailbox format of asb returns false, but it is passed for 1@a.com, which is still slightly flawed.

However, the general rules also pass that 1@a.com is a legitimate mailbox, so is there any way to verify it more accurately?

Checkdnsrr

Checkdnsrr is actually used to query the DNS record of a specified host, and we can use it to verify the existence of the mailbox.

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

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

Output:

Bool (true) bool (false)

As you can see, it is perfect, the only disadvantage is that it is too slow, after all, it is to make a network request. Therefore, it is not suitable to use this method to check a large number of mailboxes synchronously.

Filter_var+checkdnsrr

We can join filter_var and checkdnsrr for verification. For the vast majority of illegal mailboxes, they will definitely die at filter_var, and the rest will be reused.

Checkdnsrr further judged.

$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 (array_pop (explode ("@", $email), "MX") = = false) {echo "invalid email: $email\ n"; continue;}}

Output:

Invalid email: 1@a.com

Note, however, that since you are only checking the MX record, you can only judge that 163.com exists, but it does not mean that the user lastchiliarch exists.

To more accurately determine the existence of a mailbox, you have to connect to the smtp server to verify it.

This paper introduces mailbox verification and how to verify whether mailbox, URL and IP are legal with PHP. The following is an introduction:

The filter_var function is mainly used.

Grammar

Filter_var (variable, filter, options)

Variable is required. Specify the variables to filter.

Filter is optional. Specifies the ID of the filter to be used.

Options specifies an array containing flags / options. Check the possible flags and options for each filter.

PHP Filters

Example # 1 A filter_var () example

The above routine outputs:

String (15) "bob@example.com" bool (false) so far, the study on "using PHP's own method to verify the existence of a mailbox" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Mailboxes validation methods filters learning output variables legal practical more flags or help check affirm precision precision and then the host that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Tech Info MariaDB vpn Shulou Information