SMTP 连接失败并激活 TLS

SMTP Connection fails with TLS activated

我正在使用 PHPMailer 向我的客户发送电子邮件。我想使用 SSL/TLS 发送它们,但这不起作用。我正在使用来自 PHPMailer 的原始示例脚本。当我评论这一行时:$mail->SMTPSecure = 'tls'; 并将凭据更改为我的非 ssl SMTP,它工作正常。否则我会得到这个错误:

"Message was not sent.Mailer error: SMTP connect() failed."

我已经用谷歌搜索并发现了类似的问题,在 php.ini 中取消注释 ;extension=php_openssl.dll 这一行会有所帮助。但是取消注释这对我没有帮助,我检查了 phpinfo() 已经启用了 openssl,所以它应该工作还是?这是我在我的 phpinfo() 中看到的:http://puu.sh/fyg5Z/a1ab5b0ea5.png

我使用的示例(ofc 使用我自己的 smtp 凭据):

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;                                    // TCP port to connect to

$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
?>

我该如何解决?我用我的电子邮件客户端 thunderbird 测试了 SMTP TLS 连接,它工作正常。

尝试将端口 465 用于 SMTPS(加密 SMTP)

参见:

https://www.fastmail.com/help/technical/ssltlsstarttls.html

http://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol

我在 phpmailer github 的已关闭问题部分找到了答案。我将引用开发者 Synchro:

You can't use ssl with port 587. The only combinations that will work are tls/587 and ssl/465, but you should use TLS. It doesn't make any differnce if you site uses http or https.

您可以使用 php tls/ssl stram 套接字发送电子邮件(此示例使用 STARTTLS 命令启动与 smtp 服务器的 tls/ssl 连接)。这很简单,你只需要在这里创建带有附件和内联图像的 mime 消息示例 https://github.com/breakermind/PhpMimeParser/blob/master/PhpMimeClient_class.php:

<?php
// Login email and password
$login = "your-email@cool.xx";
$pass = "123456";

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'verify_peer', false);
stream_context_set_option($ctx, 'ssl', 'verify_peer_name', false);
try{
    // echo $socket = stream_socket_client('ssl://smtp.gmail.com:587', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
    echo $socket = stream_socket_client('tcp://smtp.gmail.com:587', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
    if (!$socket) {
        print "Failed to connect $err $errstr\n";
        return;
    }else{
        // Http
        // fwrite($socket, "GET / HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\n\r\n");
        // Smtp
        echo fread($socket,8192);
        echo fwrite($socket, "EHLO cool.xx\r\n");
        echo fread($socket,8192);

        // Start tls connection
        echo fwrite($socket, "STARTTLS\r\n");
        echo fread($socket,8192);

        echo stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);

        // Send ehlo
        echo fwrite($socket, "EHLO cool.xx\r\n");
        echo fread($socket,8192);

        // echo fwrite($socket, "MAIL FROM: <hello@cool.com>\r\n");
        // echo fread($socket,8192);

        echo fwrite($socket, "AUTH LOGIN\r\n");
        echo fread($socket,8192);

        echo fwrite($socket, base64_encode($login)."\r\n");
        echo fread($socket,8192);

        echo fwrite($socket, base64_encode($pass)."\r\n");
        echo fread($socket,8192);

        echo fwrite($socket, "rcpt to: <to-email@boome.com>\r\n");
        echo fread($socket,8192);

        echo fwrite($socket, "DATA\n");
        echo fread($socket,8192);

        echo fwrite($socket, "Date: ".time()."\r\nTo: <to-email@boome.com>\r\nFrom:<zour-email@cool.xx\r\nSubject:Hello from php socket tls\r\n.\r\n");
        echo fread($socket,8192);

        echo fwrite($socket, "QUIT \n");
        echo fread($socket,8192);

        /* Turn off encryption for the rest */
        // stream_socket_enable_crypto($fp, false);

        fclose($socket);
    }
}catch(Exception $e){
    echo $e;
}

Pear::mail

中禁用 SMTP TLS

编辑下面提到的路径中的文件。

/opt/lampp/lib/php/Net_SMTP/Net/SMTP.php

找到包含以下内容的行,

function auth($uid, $pwd, $method = '', $tls = true, $authz = '')

将其替换为:

function auth($uid, $pwd, $method = '', $tls = false, $authz = '')