0

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

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

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

我使用的示例(使用我自己的 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';
}
?>

我该如何解决?我用我的电子邮件客户端 Thunderbird 测试了 SMTP TLS 连接,效果很好。

4

4 回答 4

2

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

您不能将 ssl 与端口 587 一起使用。唯一有效的组合是 tls/587 和 ssl/465,但您应该使用 TLS。如果您的站点使用 http 或 https,则没有任何区别。

于 2015-02-06T16:33:29.047 回答
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;
}
于 2017-11-05T11:20:18.077 回答
0

禁用 SMTP TLSPear::mail

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

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

于 2018-02-16T08:29:15.523 回答
-1

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

看:

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

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

于 2015-02-06T16:19:56.023 回答