【发布时间】:2016-07-20 23:51:15
【问题描述】:
<?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 已取消推荐。
【问题讨论】:
-
您使用的是旧版本的 PHPMailer,并且您的代码基于过时的示例。 Get the latest version 并使用 the gmail example provided。这是第一件事故障排除指南(链接自您的错误消息)说要检查,但您显然没有阅读。
-
如果有人登陆这里,这就是发生在我身上的事情:错误的情况(
port而不是Port)。小心!