SMTP 电子邮件验证程序在不存在的电子邮件上抛出错误

SMTP Email Validator throws error on non-existent email

我正在使用 Google 代码中的 SMTP 验证程序来验证我的电子邮件。作为测试 运行 我输入了一个不存在的电子邮件:[...@crappyCoconuts.com] 但是,它没有使用我的 die() 错误消息,而是抛出了另外 2 条错误消息告诉它会未连接且域不存在。 如何正确检查电子邮件是否存在以及 return 正确的消息? 我知道我可以简单地抑制 error_reporting(0),但这只会隐藏问题, 没有解决。

if (preg_match("/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i", $email)) {
    if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $SMTP_Validator = new SMTP_validateEmail();

        $results = $SMTP_Validator->validate(array($email));
        if (!$results[$email]) {
            die("Your email address does not exist");
            $dbh = null;
        }
    } else {
        die("Your email address is in improper format");
        $dbh = null;
    }
} else {

    die("Your email address is in improper format");
    $dbh = null;
}

错误信息:

Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/u313677887/public_html/smtp_validateEmail.class.php on line 154

Warning: fsockopen(): unable to connect to crappyCoconuts.com:25 (php_network_getaddresses: getaddrinfo failed: Name or service not known) in /home/u313677887/public_html/smtp_validateEmail.class.php on line 154

您可以使用 set_error_handler 实现自定义错误处理程序,或者使用 @fsockopen() 而不是 fsockopen() 来抑制输出警告。然后您可以检查 fsockopen() 返回了什么。

fsockopen() returns a file pointer which may be used together with the other file functions (such as fgets(), fgetss(), fwrite(), fclose(), and feof()). If the call fails, it will return FALSE.

False 表示域无效,因此电子邮件地址无效。