我收到此错误
致命错误:未捕获的异常' phpmailerException'与消息 ' SMTP connect()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting'在 C:\ XAMPP \ htdocs中\ muhasibb \电子纸\ PHPMailer的主\ class.phpmailer.php:1465 堆栈跟踪:#0 C:\ XAMPP \ htdocs中\ muhasibb \电子纸\ PHPMailer的主\ class.phpmailer.php(1301): PHPMailer-> smtpSend(' Date:Tue,17 O ...','这是Yousaf ......')#1 C:\ XAMPP \ htdocs中\ muhasibb \电子纸\ PHPMailer的主\ class.phpmailer.php(1181): PHPMailer-> postSend()#2 C:\ xampp \ htdocs \ muhasibb \ epaper \ 1.php(33): PHPMailer-> send()#3 {main}抛出 C:\ XAMPP \ htdocs中\ muhasibb \电子纸\ PHPMailer的主\ class.phpmailer.php 在第1465行
我正在使用代码
<?php
// Passing `true` enables exceptions
//Server settings
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer(true); // Enable verbose debug output
$mail->isSMTP();
$mail->SMTPDebug = 2; // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'yousaf.farooq906@gmail.com'; // SMTP username
$mail->Password = '********'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
$mail->Mailer = "smtp"; // TCP port to connect to
//Recipients
$mail->setFrom('yousaf.farooq906@gmail.com', 'Yousaf Farooq');
$mail->addAddress('yousaf.farooq906@gmail.com', 'Joe User'); // Add a recipient
$mail->addAddress('ellen@example.com'); // Name is optional
//Attachments
// Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Yousaf Farooq';
$mail->Body = 'This is Yousaf Farooq';
if($mail->send())
echo 'Message has been sent';
else
echo 'Message could not be sent.';
?>
答案 0 :(得分:1)
尝试设置
$mail->SMTPSecure
到
$mail->SMTPSecure = 'tls';
此外,为您的Google帐户创建应用密码,并在$mail->Password
中使用它来换取您的密码。
答案 1 :(得分:1)
此代码对我有用
$mail = new PHPMailer(true); // create a new object
我评论了这一行 // $ mail-> isSMTP(); //启用SMTP
$mail->SMTPDebug = 4;
$mail->SMTPAuth = true; // authentication enabled
您必须使用tls 587端口
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Mailer = "smtp";
$mail->isHTML(true);
$mail->Username = "yourmail@gmail.com";
$mail->Password = "yourpassword";
$mail->From="yourmail@gmail.com";
$mail->FromName="YOUR NAME";
$mail->Subject = $subject;
$mail->Body = $body;
$mail->addAddress("somemail@gmail.com");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Email Sent";
}
此外,当您使用gmail时,请转到具有帐户访问权限的应用并设置“允许不太安全的应用:打开”
答案 2 :(得分:-1)
如果在localhost中工作,
在php.ini中:删除&#34; sendmail_from = postmaster @ localhost&#34;前面的分号。并重新启动xampp。
在Gmail帐户中:在安全设置中,启用&#34;允许安全性较低的应用&#34;。
最后在PHP脚本中:添加两个函数: &#34; date_default_timezone_set(&#39;其他/ UTC&#39;)&#34; &#34;的gethostbyname(&#39; SSL://smtp.gmail.com')&#34;作为&#34;的价值$ MAIL-&GT;主机&#34;