PHPMailer SMTP错误:无法连接到服务器 [英] PHPMailer SMTP ERROR: Failed to connect to server
本文介绍了PHPMailer SMTP错误:无法连接到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<?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
我将这个php托管在我的xampp本地服务器上. php.ini
上的extension=php_openssl.dll
已经不被推荐.
I am hosting this php on my xampp local server. The extension=php_openssl.dll
on php.ini
is already uncommended.
推荐答案
您的配置可能有误.我相信,如果将主机更改为smtp.gmail.com
,可能会解决您的问题.
Your configuration might be wrong. I believe that if you change your host into smtp.gmail.com
it might solve your problem.
我注意到您设置了安全性tls,但是您也想与ssl连接.
I noticed that you set security tls but you want to connect with ssl as well.
将$mail->Host = "ssl://smtp.gmail.com";
更改为$mail->Host = "smtp.gmail.com";
,并将安全性更改为ssl.
Change $mail->Host = "ssl://smtp.gmail.com";
into $mail->Host = "smtp.gmail.com";
and security to ssl.
来自此答案:
$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错误:无法连接到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文