SMTP连接失败
问题描述:
在这个脚本完美运行之前,我最近三天面临这个问题。现在收到错误:SMTP连接失败
SMTP ERROR: Failed to connect to server: (0) 2017-10-06 21:05:34 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 [email protected]
这里是我的脚本:
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 2; // Enable verbose debug output
$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 = '[email protected]'; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
$mail->setFrom('[email protected]', 'Your Name');
$mail->addAddress('[email protected]', 'My Friend');
$mail->Subject = 'First PHPMailer Message';
$mail->Body = 'Hi! This is my first e-mail sent through PHPMailer.';
if (!$mail->send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
我认为Gmail可能已发送使用SMTP,或类似的东西电子邮件更改的设置。
答
最后我能够从本地主机发送电子邮件。这是我的代码。
要安装:
- 下载PHPMailer的
- 将它添加到您的项目(我把它放在根目录)
- 添加自动加载类的脚本。
-
休息的代码如下
require "PHPMailer/PHPMailerAutoload.php"; $mail = new PHPMailer; $mail->SMTPOptions = array( 'ssl' => array( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true ) ); //$mail->SMTPDebug = 2; // Enable verbose debug output $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 = '[email protected]'; // SMTP username $mail->Password = 'securepass'; // SMTP password $mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted $mail->Port = 465; // TCP port to connect to //Recipients $mail->setFrom('[email protected]', "Mailer"); $mail->addAddress("[email protected]","receiver Name"); $mail->isHTML(true); $mail->Subject = "Subject"; $mail->Body = "Body"; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; if($mail->send()){ return array("msg"=>msg("success","Email has been sent.<br>")); } else { return array("msg"=>msg("error","Email can't send.Try Again<br>")); }
这里有一个大胆的想法 - 尝试在错误信息,告诉你所有关于这个问题以及如何解决它的链接如下。 – Synchro