[英]Uncaught exception 'PHP mailer Exception' with message SMTP connect() failed
I am getting this error 我收到此错误
Fatal error: Uncaught exception 'phpmailerException' with message 'SMTP connect() failed. 致命错误:消息“ SMTP connect()”未捕获的异常“ phpmailerException”失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting ' in C:\\xampp\\htdocs\\muhasibb\\epaper\\PHPMailer-master\\class.phpmailer.php:1465 Stack trace: #0 C:\\xampp\\htdocs\\muhasibb\\epaper\\PHPMailer-master\\class.phpmailer.php(1301): PHPMailer->smtpSend('Date: Tue, 17 O...', 'This is Yousaf ...') #1 C:\\xampp\\htdocs\\muhasibb\\epaper\\PHPMailer-master\\class.phpmailer.php(1181): PHPMailer->postSend() #2 C:\\xampp\\htdocs\\muhasibb\\epaper\\1.php(33): PHPMailer->send() #3 {main} thrown in C:\\xampp\\htdocs\\muhasibb\\epaper\\PHPMailer-master\\class.phpmailer.php on line 1465 https://github.com/PHPMailer/PHPMailer/wiki/在C:\\ xampp \\ htdocs \\ muhasibb \\ epaper \\ PHPMailer-master \\ class.phpmailer.php:1465中对问题进行故障排除:#0 C:\\ xampp \\ htdocs \\ muhasibb \\ epaper \\ PHPMailer-master \\ class.phpmailer.php(1301):PHPMailer-> smtpSend('Date:Tue,17 O ...','This is Yousaf ...')#1 C:\\ xampp \\ htdocs \\ muhasibb \\ epaper \\ PHPMailer-master \\ class.phpmailer.php(1181):PHPMailer-> postSend()#2 C:\\ xampp \\ htdocs \\ muhasibb \\ epaper \\ 1.php(33):PHPMailer-> send ()在第1465行的C:\\ xampp \\ htdocs \\ muhasibb \\ epaper \\ PHPMailer-master \\ class.phpmailer.php中抛出#3 {main}
while i am using code 当我使用代码时
<?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.';
?>
try setting your 尝试设置您的
$mail->SMTPSecure
to 至
$mail->SMTPSecure = 'tls';
Also, create app password for your google account and use it in exchange for your password in $mail->Password
. 另外,为您的Google帐户创建应用密码,并用它来代替$mail->Password
。
This code worked for me 这段代码对我有用
$mail = new PHPMailer(true); // create a new object
I commented this line //$mail->isSMTP(); 我评论了这一行// $ mail-> isSMTP(); // enable SMTP //启用SMTP
$mail->SMTPDebug = 4;
$mail->SMTPAuth = true; // authentication enabled
You have to use tls whith port 587 您必须使用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";
}
Also when you are using gmail you go to Apps with account access and set Allow less secure apps: ON 另外,当您使用gmail时,请转到具有帐户访问权限的应用并设置“允许不太安全的应用”:
If working in localhost, 如果在本地主机上工作,
In php.ini: remove the semicolon in front of "sendmail_from = postmaster@localhost" and restart xampp. 在php.ini中:删除“ sendmail_from = postmaster @ localhost”前面的分号,然后重新启动xampp。
In gmail account: in the security settings, turn on "Allow less secure apps". 在gmail帐户中:在安全设置中,打开“允许安全程度较低的应用”。
And finally in PHP script: add two functions: "date_default_timezone_set('Etc/UTC')" "gethostbyname('ssl://smtp.gmail.com')" as a value for " $mail->Host" 最后在PHP脚本中:添加两个函数:“ date_default_timezone_set('Etc / UTC')”“ gethostbyname('ssl://smtp.gmail.com')”作为“ $ mail-> Host”的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.