SMTPDebug = 2; $mail->Hos-6ren">
gpt4 book ai didi

PHPMailer SMTP 错误 : Failed to connect to server

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:40:48 24 4
gpt4 key购买 nike

<?php

include("class.phpmailer.php");
include("class.smtp.php");

$mail = new PHPMailer();

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Mailer = "smtp";
$mail->SMTPDebug = 2;
$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server
$mail->Port = 587; // set the port to use
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = "tls";

$mail->Username = "123@gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password

$mail->From = "123@gmail.com";
$mail->FromName = "Webmaster";

$mail->AddAddress("asd@hotmail.com");
$mail->AddReplyTo("123@gmail.com", "Webmaster");
$mail->IsHTML(true);

$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;

if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>

返回错误

2016-04-01 08:41:43 SMTP ERROR: Failed to connect to server: (0) 
2016-04-01 08:41:43 SMTP connect() failed.

https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message was not sent.Mailer error: SMTP connect() failed.

https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

我在我的 xampp 本地服务器上托管此 php。 php.ini 上的 extension=php_openssl.dll 已不推荐。

最佳答案

您的配置可能有误。我相信,如果您将主机更改为 smtp.gmail.com,它可能会解决您的问题。

我注意到您设置了安全 tls,但您也想连接 ssl。

$mail->Host = "ssl://smtp.gmail.com"; 更改为 $mail->Host = "smtp.gmail.com"; 和 SSL 的安全性。

来自 this answer :

$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "email@gmail.com";
$mail->Password = "password";
$mail->SetFrom("example@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email@gmail.com");

if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}

关于PHPMailer SMTP 错误 : Failed to connect to server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36351817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com