如何使用 PHP 检查电子邮件地址是否真实或有效

如何使用 PHP 检查电子邮件地址是否真实或有效

问题描述:

我发现一些网站声称可以验证电子邮件地址是否有效.是否可以仅使用 PHP 来检查电子邮件地址是否有效?

I found some websites that claim to verify if email addresses are valid. Is it possible to check if an email address is valid using just PHP?

<?php
    if($_POST['email'] != ''){
        // The email to validate
        $email = $_POST['email'];

        // An optional sender
        function domain_exists($email, $record = 'MX'){
            list($user, $domain) = explode('@', $email);
            return checkdnsrr($domain, $record);
        }
        if(domain_exists($email)) {
            echo('This MX records exists; I will accept this email as valid.');
        }
        else {
            echo('No MX record exists;  Invalid email.');
        }
    }
?>
<form method="POST">
    <input type="text" name="email">
    <input type="submit" value="submit">
</form>

这就是我现在所拥有的.它检查域是否存在但它不能检查域是否存在用户的电子邮件存在于该域中.是否可以使用 PHP 来做到这一点?

This is what I have right now. It checks if the domain exist, but it cannot check if the user's email exist on that domain. Is it possible to do that using PHP?

您应该检查 SMTP.

You should check with SMTP.

这意味着您必须连接到该电子邮件的 SMTP 服务器.

That means you have to connect to that email's SMTP server.

连接到 SMTP 服务器后,您应该发送以下命令:

After connecting to the SMTP server you should send these commands:

HELO somehostname.com
MAIL FROM: <no-reply@gmail.com>
RCPT TO: <emailtovalidate@domain.com>

如果您收到 Relay access denied",则表示此电子邮件无效.

If you get "<emailtovalidate@domain.com> Relay access denied" that means this email is Invalid.

有一个简单的 PHP 类.你可以使用它:

There is a simple PHP class. You can use it:

http://www.phpclasses.org/package/6650-PHP-Check-if-an-e-mail-is-valid-using-SMTP.html