【发布时间】:2018-11-17 09:17:48
【问题描述】:
我收到了这个错误
致命错误:带有消息的未捕获异常“phpmailerException” 'SMTP 连接()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting'在 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() #3 {main} 抛出 C:\xampp\htdocs\muhasibb\epaper\PHPMailer-master\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.';
?>
【问题讨论】:
-
既然你已经要求它抛出异常(通过将
true传递给构造函数),尝试捕获它们可能是个好主意。您还使用的是旧版本的 PHPMailer,并且您的代码没有基于它提供的 gmail 示例,它不会犯这些错误。 -
大部分内容都包含在错误消息链接到的故障排除指南中。