简体   繁体   English

SMTP连接失败并激活了TLS

[英]SMTP Connection fails with TLS activated

I am using the PHPMailer to send E-Mails to my customers. 我正在使用PHPMailer向我的客户发送电子邮件。 I want to send them using SSL/TLS, but this doesn't work. 我想使用SSL / TLS发送它们,但这不起作用。 I am using an original example script from PHPMailer. 我正在使用PHPMailer的原始示例脚本。 When I comment this line: $mail->SMTPSecure = 'tls'; 当我评论以下行时: $mail->SMTPSecure = 'tls'; and change the credentials to my non ssl SMTP it works fine. 并将凭据更改为我的非SSL SMTP,效果很好。 Otherwise I get this error: 否则我得到这个错误:

"Message was not sent.Mailer error: SMTP connect() failed." “未发送消息。邮件程序错误:SMTP connect()失败。”

I googled already and found similar problems where uncommenting ;extension=php_openssl.dll this line in the php.ini helped. 我已经在google上搜索了,发现了类似的问题,取消注释;extension=php_openssl.dll帮助php.ini中的这一行。 But uncommenting this doesn't help me and I checked with phpinfo() that openssl is already enabled, so it should work or ? 但是取消注释对此无济于事,我与phpinfo()一起检查了openssl已启用,因此它应该起作用还是? This is what I see at my phpinfo(): http://puu.sh/fyg5Z/a1ab5b0ea5.png 这是我在phpinfo()中看到的内容: http : //puu.sh/fyg5Z/a1ab5b0ea5.png

My used example (ofc with my own smtp credentials): 我使用的示例(带有我自己的smtp凭据的OFC):

<?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';
}
?>

How can I fix it? 我该如何解决? I tested the SMTP TLS connection with my email client thunderbird, which worked fine. 我使用电子邮件客户端雷鸟测试了SMTP TLS连接,效果很好。

I found the answer in the closed issues section of the phpmailer github. 我在phpmailer github的封闭问题部分中找到了答案。 I will quote the developer Synchro: 我将引用开发商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. 您不能将ssl与端口587一起使用。唯一适用的组合是tls / 587和ssl / 465,但您应该使用TLS。 It doesn't make any differnce if you site uses http or https. 如果您的站点使用http或https,则没有任何区别。

You can send email with php tls/ssl stram socket (this example using STARTTLS command to start tls/ssl connection to smtp server). 您可以使用php tls / ssl stram套接字发送电子邮件(此示例使用STARTTLS命令启动tls / ssl与smtp服务器的连接)。 It is very simple, you need only create mime message here example https://github.com/breakermind/PhpMimeParser/blob/master/PhpMimeClient_class.php with attachments and inline images: 这很简单,您只需要在此处创建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;
}

Disable SMTP TLS in Pear::mail Pear::mail禁用SMTP TLS

Edit the file in below mentioned path. 在下面提到的路径中编辑文件。

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

Find the line with the following content, 找到包含以下内容的行,

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

Replace this with: 替换为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM