<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.gmail.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'mygmailpassword'; // SMTP password
$mail->Port = 465; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom('[email protected]', 'Courses');
$mail->addAddress('[email protected]'); // Add a recipient
$mail->addReplyTo('[email protected]', 'No-reply');
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
这是我得到的错误
2020-05-06 07:59:56 SERVER -> CLIENT:
2020-05-06 07:59:56 SMTP NOTICE: EOF caught while checking if connected
SMTP Error: Could not authenticate.
SMTP Error: Could not authenticate.
Message could not be sent. Mailer Error: SMTP Error: Could not authenticate.
我把端口改成了465改成了587,但是没有用。我搜索了一下这个问题,但大部分答案都是端口改成了465,但没有用。我在代码中做错了什么.对不起,我是初学者。
试着加上这个。这为我解决了问题!
$mail->SMTPSecure = 'ssl';
确保你没有开启两步验证。
值得注意的是,有些SMTP服务器会阻止连接。有些SMTP服务器不支持SSL(或TLS)连接。