我正在与phpmailer和smtp一起从本地主机向gmail发送邮件。问题是,首先我发送邮件,并且工作正常,但现在给smtp连接失败。这是我的代码。我对此很陌生,请帮帮我。
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'smag.ghaznavi1@gmail.com'; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->SMTPDebug=2;
$mail->setFrom('info@example.com', 'PMS');
//$mail->addReplyTo('info@example.com', 'CodexWorld');
$mail->addAddress('smag.ghaznavi1@gmail.com'); // Add a recipient
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
$mail->isHTML(true); // Set email format to HTML
$bodyContent = '<h1>Your password reset link</h1>';
$bodyContent .= "Dear user,\n\nIf this e-mail does not apply to you please ignore it. It appears that you have requested a password reset at our website www.yoursitehere.com\n\nTo reset your password, please click the link below. If you cannot click it, please paste it into your web browser's address bar.\n\n" . $pwrurl . "\n\nThanks,\nThe Administration";
$mail->Subject = 'Email from Project Management System Admin';
$mail->Body = $bodyContent;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
smag.ghaznavi1@gmail.com2018-07-04 16:43:58 SERVER -> CLIENT: 220
smtp.gmail.com ESMTP o4-v6sm5724500wmo.20 - gsmtp
2018-07-04 16:43:58 CLIENT -> SERVER: EHLO localhost
2018-07-04 16:43:58 SERVER -> CLIENT: 250-smtp.gmail.com at your service,
[39.54.130.23]250-SIZE 35882577250-8BITMIME250-STARTTLS250-
ENHANCEDSTATUSCODES250-PIPELINING250 SMTPUTF8
2018-07-04 16:43:58 CLIENT -> SERVER: STARTTLS
2018-07-04 16:43:58 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2018-07-04 16:43:59 CLIENT -> SERVER: QUIT
2018-07-04 16:43:59 SERVER -> CLIENT:
2018-07-04 16:43:59 SMTP ERROR: QUIT command failed:
SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message could not be sent.Mailer Error: SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
答案 0 :(得分:0)
答案 1 :(得分:0)
无法启动TLS,这通常表明您的CA证书已过期,尤其是在使用Gmail时。这与“安全性较低的应用”设置或验证码问题无关,尽管您稍后可能会遇到这些问题。
在错误消息中遵循the link to the troubleshooting guide,该消息描述了此确切错误,并告诉您如何解决(通过更新CA证书-通过禁用证书验证来不!)。真是太不可思议了,好像该链接纯粹是在帮助您解决此类问题而无需提出问题并等待答案...