我试图在本地主机上使用PHPMailer发送电子邮件。我想使用以下代码将电子邮件发送到我自己的Gmail帐户:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'C:/xampp/composer/vendor/autoload.php';
include_once('C:/xampp/phpmailer-master/class.phpmailer.php');
require_once('C:/xampp/phpmailer-master/class.smtp.php');
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com;';
$mail->SMTPAuth = true;
$mail->username = 'email@gmail.com';
$mail->Password = 'password1234';
$mail->SMTPSecure = true;
$mail->Port = 587;
$mail->From = "email@gmail.com";
$mail->FromName = "Wide World";
$mail->smtpConnect(
array(
"ssl" => array(
"verify_peer" => false,"verify_peer_name" => false,"allow_self_signed" => true
)
)
);
$mail->addAddress("email@gmail.com");
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
echo "Mail has been sent succesfully!";
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
但是,当我在浏览器中运行它时,输出显示该电子邮件已成功发送,没有显示任何错误。我已经检查了Apache中的错误日志,它也是空的。
2019-11-26 14:05:18 Connection: opening to smtp.gmail.com:587,timeout=300,options=array ( 'ssl' => array ( 'verify_peer' => false,'verify_peer_name' => false,'allow_self_signed' => true,),)
2019-11-26 14:05:18 Connection: opened
2019-11-26 14:05:18 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP r2sm14848179wrp.64 - gsmtp
2019-11-26 14:05:18 CLIENT -> SERVER: EHLO localhost
2019-11-26 14:05:18 SERVER -> CLIENT: 250-smtp.gmail.com at your service,[145.44.68.125]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIpelINING250-CHUNKING250 SMTPUTF8
2019-11-26 14:05:18 CLIENT -> SERVER: STARTTLS
2019-11-26 14:05:18 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2019-11-26 14:05:18 CLIENT -> SERVER: EHLO localhost
2019-11-26 14:05:18 SERVER -> CLIENT: 250-smtp.gmail.com at your service,[145.44.68.125]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIpelINING250-CHUNKING250 SMTPUTF8
2019-11-26 14:05:18 CLIENT -> SERVER: AUTH LOGIN
2019-11-26 14:05:18 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2019-11-26 14:05:18 CLIENT -> SERVER: [credentials hidden]
2019-11-26 14:05:18 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2019-11-26 14:05:18 CLIENT -> SERVER: [credentials hidden]
2019-11-26 14:05:19 SERVER -> CLIENT: 235 2.7.0 accepted
Mail has been sent succesfully!2019-11-26 14:05:19 CLIENT -> SERVER: QUIT
2019-11-26 14:05:19 SERVER -> CLIENT: 221 2.0.0 closing connection r2sm14848179wrp.64 - gsmtp
2019-11-26 14:05:19 Connection: closed
它没有显示错误,但没有发送任何内容。
请帮助我:)