php - SMTP 连接失败并激活 TLS

标签 php email ssl phpmailer starttls

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

“消息未发送。邮件程序错误:SMTP connect() 失败。”

我已经用谷歌搜索并发现了类似的问题,其中取消注释 ;extension=php_openssl.dll php.ini 中的这一行有帮助。但是取消注释这对我没有帮助,我检查了 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 连接,它运行良好。

最佳答案

我在 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 - SMTP 连接失败并激活 TLS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28369938/

相关文章:

php - 使用 PHP 获取排序查询的第一行和第二行中的值之间的差异

php - 计算php中2个时间戳之间的差异

php - 在保持表格结构的同时垂直显示表格值

java - 如何从需要在 JAVA 中进行身份验证的 HTTP 代理后面的 Gmail 帐户发送电子邮件?

java - 如何使用 Java Mail API 检索任何电子邮件的电子邮件正文

kotlin - 如何禁用 Ktor 客户端 SSL 验证?

php - : $_SERVER ['SCRIPT_NAME' ] and $_SERVER ['PHP_SELF' ] 之间的区别

php - 我需要什么才能获得合规的电子邮件 header

wcf - 在 IIS 中通过 SSL 进行自定义基本身份验证的 REST WCF 4 服务

jquery - 在 IE9 上托管 jquery、ssl