使用XAMPP的phpMail无法连接到SMTP主机
我试图在本地主机服务器上使用PHPMailer和XAMPP,我使用的是Gmail SMTP,但是SMTP不会连接。有人能帮我找到问题吗?我使用的是他们在Github上的Gmail示例。正如故障排除指南所说,当我更改SMTPOptions时,问题得到了解决,但是,如果这是一个不安全的选项,我希望以正确的方式执行 这是mailer.php输出的错误消息,我已尝试从他们的Github进行故障排除,但似乎找不到解决方案:使用XAMPP的phpMail无法连接到SMTP主机,php,email,smtp,phpmailer,Php,Email,Smtp,Phpmailer,我试图在本地主机服务器上使用PHPMailer和XAMPP,我使用的是Gmail SMTP,但是SMTP不会连接。有人能帮我找到问题吗?我使用的是他们在Github上的Gmail示例。正如故障排除指南所说,当我更改SMTPOptions时,问题得到了解决,但是,如果这是一个不安全的选项,我希望以正确的方式执行 这是mailer.php输出的错误消息,我已尝试从他们的Github进行故障排除,但似乎找不到解决方案: 2020-03-21 18:04:57 SERVER -> CLIENT:
2020-03-21 18:04:57 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP f2sm448312ljn.101 - gsmtp
2020-03-21 18:04:57 CLIENT -> SERVER: EHLO 127.0.0.1
2020-03-21 18:04:57 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [2a02:aa7:460f:7e0c:c0fb:2279:35d7:a54a]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250 SMTPUTF8
2020-03-21 18:04:57 CLIENT -> SERVER: STARTTLS
2020-03-21 18:04:58 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2020-03-21 18:04:58 CLIENT -> SERVER: QUIT
2020-03-21 18:04:58 SERVER -> CLIENT:
2020-03-21 18:04:58 SMTP ERROR: QUIT command failed:
SMTP Error: Could not connect to SMTP host.
Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.
My settings:
PHP.ini:
SMTP=smtp.gmail.com
smtp_port=587
Sendmail.ini:
smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=auto
auth_username=EMAIL
auth_password=PASSWORD
My Mailer.php:
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
// Load Composer's autoloader
require 'PHPMailer/vendor/autoload.php';
// Instantiation and passing `true` enables exceptions
$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 = 'EMAIL'; // SMTP username
$mail->Password = 'PASSWORD'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 587; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom('EMAIL', 'NAME');
$mail->addAddress('EMAIL', '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->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
查看此答案,您需要允许google帐户进行不太安全的连接。请参阅此答案,这不是问题所在。它在STARTTLS之后立即失败,因此很可能是您的CA证书已过期,或者ISP正在重定向SMTP通信,因此验证失败(应该如此)。这两个问题在中都有非常详细的描述,在这里也有许多相同的问题-请!我已经允许我的谷歌帐户进行不太安全的连接。当我按照故障排除指南中的说明更改SMTP选项时,它会起作用,但它们指出这是一个不太安全的选项,因此,我希望以正确的方式实现它。