【发布时间】:2021-10-13 12:00:42
【问题描述】:
在过去的四个小时里,我一直在阅读示例、观看 YouTube 视频等,但我终生无法收到要发送的电子邮件。我已尝试使用更改设置的 G-mail 帐户以允许使用不太安全的应用程序以及我公司的私人托管电子邮件地址。
我用 composer 加载了 PHPMailer
当我使用下面的代码(实际上是 PHPMailer 的 github 页面中的示例)时,我得到了下面列出的错误。
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require_once 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.gmail.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'my gmail email here'; //SMTP username
$mail->Password = 'my gmail password here'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
//Recipients
$mail->setFrom('my gmail email address', 'Mailer');
$mail->addAddress('different email address', 'Joe User'); //Add a recipient
//Content
$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';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
这是我收到的错误消息: 2021-08-09 20:04:25 SMTP 错误:无法连接到服务器:(0) SMTP 连接()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting 无法发送消息。邮件程序错误:SMTP 连接()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
我 100% 确定电子邮件地址和密码正确并列在正确的位置。
有谁知道发生了什么或我可以做些什么来解决?
我非常绝望,我正要切换编程语言,因为这是我遇到过的最复杂的事情!
非常感谢任何有解决方案或提示的人!
PS我目前使用的是localhost...我会回答您需要回答的任何问题谢谢!!!
更新>>> 当我改为这一行时
$mail->SMTPDebug = SMTP::DEBUG_CONNECTION;
我得到这个输出:
2021-08-09 21:02:10 连接:打开 ssl://smtp.gmail.com:465, timeout=300, options=array() 2021-08-09 21:02:10 连接失败。错误 #2:stream_socket_client():SSL 操作失败,代码 1。OpenSSL 错误消息:错误:1416F086:SSL 例程:tls_process_server_certificate:证书验证失败 [C:\xampp\htdocs\sheri\vendor\phpmailer\phpmailer\src\SMTP .php 第 388 行] 2021-08-09 21:02:10 连接失败。错误 #2:stream_socket_client():无法启用加密 [C:\xampp\htdocs\sheri\vendor\phpmailer\phpmailer\src\SMTP.php 第 388 行] 2021-08-09 21:02:10 连接失败。错误 #2:stream_socket_client():无法连接到 ssl://smtp.gmail.com:465(未知错误)[C:\xampp\htdocs\sheri\vendor\phpmailer\phpmailer\src\SMTP.php 第 388 行] 2021-08-09 21:02:10 SMTP 错误:无法连接到服务器:(0) SMTP 连接()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting 无法发送消息。邮件程序错误:SMTP 连接()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
【问题讨论】:
-
“我太绝望了,我要切换编程语言了” 注意这个问题与 PHP 无关。 “SMTP 连接失败”表示您的服务器无法连接到 Google 的服务器。您的托管服务提供商可能会阻止出站 SMTP 连接尝试以防止垃圾邮件。你使用的是什么主机?你能从命令行执行
telnet smtp.gmail.com 465并获得响应头吗? -
所以您设法花费了 4 个小时完全忽略了错误消息中的链接,该链接直接指向您如何诊断这个确切问题的详细描述?
-
@AlexHowansky 我写是因为它自己的邮件功能一文不值,而phpmailer似乎垄断了邮件。
-
检查你在和谁说话。我是 PHPMailer 的维护者,我已经在 SO 上回答了 1,000 多个关于 PHPMailer 的问题(包括你的许多重复问题)。如果您点击了该链接,为什么您没有在您坚称从未发生在任何人身上的问题中包含您的测试结果?
-
你误会了。哪个托管服务提供商提供了运行此代码的服务器?