有没有办法使用Zend-Mail验证SMTP配置?

时间:2016-07-08 21:45:57

标签: php magento zend-mail

我一直在用PHP编写一个发送邮件的类,我决定使用Zend框架。此类使用用户的SMTP配置发送邮件。截至目前,我正在使用提供的用户凭据检查用户的SMTP配置,并向“虚拟”电子邮件地址发送“虚拟”电子邮件,并捕获可能出错的ZendException类。这是一种可怕的方法,原因有很多:

  • 最终因涉嫌“垃圾邮件”(GMail)而被禁止的SMTP用户
  • 效率低,耗时长
  • 电子邮件递送尝试失败在用户邮箱中

以下是我正在做的测试SMTP配置是否有效的示例:

public function validSMTP () {
    // Get the user's SMTP configuration
    $config = $this->getConfiguration ();
    // Create a new Zend transport SMTP object
    $transport = new Zend_Mail_Transport_Smtp ( $config ["hostname"], [
        "auth"      =>  "login",
        "ssl"       =>  $config ["protocol"],
        "port"      =>  $config ["port"],
        "username"  =>  $config ["from"],
        "password"  =>  $config ["password"]
    ]);
    // Create a new message and send it to dummy email
    $mail = new Zend_Mail ("UTF-8");
    $mail->setBodyText ( "null" );
    $mail->setFrom ( $config ["from"] );
    $mail->addTo ( "dev@null.com" );
    $mail->setSubject ( "Test" );
    // Attempt to send the email
    try {
        // Send the email out
        $mail->send ( $transport );
        // If all is well, return true
        return true;
    }
    // Catch all Zend exceptions
    catch ( Zend_Exception $exception ) {
        // Invalid configuration
        return false;
    }
}

所以我的问题是:有更好的方法吗? Zend_Mail 有这样的内置功能吗?我看了一遍,在 Zend_Mail 中找不到任何内置内容。提前谢谢你的回答!

2 个答案:

答案 0 :(得分:1)

我在documentation中没有看到任何表明可以测试SMTP身份验证而无需实际尝试发送邮件的方法。也许当你调用setOptionssetConnectionConfig时,它会尝试登录并在失败时抛出异常,但我怀疑没有。

不是每次要发送邮件时执行验证检查,而是将配置数据和验证结果保存在文件中。然后在进行SMTP检查之前,请阅读该文件并检查配置设置是否相同。如果是,只需返回保存的结果。

这解决了问题列表中的前两项,如果配置无效,他们只会第一次收到退回邮件。

答案 1 :(得分:0)

我决定手动检查,我会在下面发布最终结果,以便将来可以帮助某人。感谢@Barmar帮助我搜索文档并得出结果 Zend_Mail 框架不支持这样的功能。不幸的是,以下方法仅适用于SSL,因为TLS涉及在EHLO命令之后加密(以及更多)。如果配置有效,则返回 true ,否则返回字符串形式无效的原因。

public function validSMTP () {
        // Initialize client configuration, for this example
        $config = [
            "hostname"     =>     "smtp.gmail.com",
            "port"         =>     465,
            "protocol"     =>     "ssl",
            "username"     =>     "USERNAME",
            "password"     =>     "PASSWORD",
        ];
        // Add the connection url based on protocol
        $config ["url"] = $config ["protocol"] . "://" . $config ["hostname"];
        // Open a new socket connection to the SMTP mail server
        if ( !( $socket = fsockopen ( $config ["url"], $config ["port"], $en, $es, 15 ) ) ) {
            // Could not establish connection to host
            return "Connection failed, check hostname/port?";
        }
        // Make sure that the protocol is correct
        if ( $this->status_match($socket, '220') === false ) {
            // Probably a wrong protocol (SSL/TLS)
            return "Connection failed, check protocol?";
        }
        // Send hello message to server
        fwrite ( $socket, "EHLO " . $config ["hostname"] ."\r\n" );
        if ( $this->status_match ( $socket, "250" ) === false ) {
            // If Hello fails, say config is invalid
            return "Invalid SMTP configuration";
        }
        // Request to login
        fwrite ( $socket, "AUTH LOGIN\r\n" );
        if ( $this->status_match ( $socket, "334" ) === false ) {
            return "Invalid SMTP configuration";
        }
        // Send the username
        fwrite ( $socket, base64_encode ( $config [ "username" ] ) . "\r\n" );
        if ( $this->status_match ( $socket, "334" ) === false ) {
            // If failed, warn that invalid username password was passed
            return "Invalid username/password combination.";
        }
        // Send the password
        fwrite ( $socket, base64_encode ( $config [ "password" ] ) . "\r\n" );
        if ( $this->status_match ( $socket, "235" )  === false ) {
            // If failed, warn that invalid username password was passed
            return "Invalid username/password combination.";
        }
        // Close the socket connections
        fclose ( $socket );
        // Return true, if everything above passed
        return true;
    }

    private function status_match ( $socket, $expected ) {
        // Initialize the response string
        $response = '';
        // Get response until nothing but code is visible
        while ( substr ( $response, 3, 1) != ' ' ) {
            // Receive 250 bytes
            if ( !( $response = fgets ( $socket, 256 ) ) ) {
                // Break if nothing else to read
                break;
            }
        }
        // If the status code is not what was expected
        if ( !( substr ( $response, 0, 3 ) == $expected ) ) {
            // Return false
            return false;
        }
        // Otherwise return true
        return true;
    }

我通过使用以下示例得出了这个:http://schoudhury.com/blog/articles/send-email-using-gmail-from-php-with-fsockopen/