激活TLS后,SMTP连接失败

时间:2015-02-06 16:08:06

标签: php email ssl phpmailer starttls

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

“邮件未发送。邮件程序错误:SMTP连接()失败。”

我已经google了,发现类似的问题,在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连接,该工作正常。

4 个答案:

答案 0 :(得分:2)

我在phpmailer github的封闭问题部分找到了答案。我将引用开发人员Synchro的话:

  

您不能将ssl与端口587一起使用。唯一可行的组合   是tls / 587和ssl / 465,但你应该使用TLS。它没有做任何事情   如果您的网站使用http或https,则会有所不同。

答案 1 :(得分:0)

您可以使用php tls / ssl stram socket发送电子邮件(此示例使用STARTTLS命令启动tls / ssl连接到smtp服务器)。这很简单,你只需要在这里创建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;
}

答案 2 :(得分:0)

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 = '')

答案 3 :(得分:-1)