如何使用 PHP 检查电子邮件地址是否真实或有效 [英] How to check if an email address is real or valid using PHP

查看:44
本文介绍了如何使用 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

这篇关于如何使用 PHP 检查电子邮件地址是否真实或有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆