【发布时间】:2021-09-12 10:02:31
【问题描述】:
几天后我在 XAMPP 中卡住了电子邮件功能,我正在尝试使用 PHPMailer 函数在 XAMPP 中发送电子邮件,但它无法正常工作,
错误出现在这些消息下方:
2021-06-30 08:29:38 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP b25sm12430661ios.36 - gsmtp
2021-06-30 08:29:38 CLIENT -> SERVER: EHLO localhost
2021-06-30 08:29:38 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [157.230.55.84]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250 SMTPUTF8
2021-06-30 08:29:38 CLIENT -> SERVER: STARTTLS
2021-06-30 08:29:38 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2021-06-30 08:29:39 CLIENT -> SERVER: QUIT
2021-06-30 08:29:39 SERVER -> CLIENT:
2021-06-30 08:29:39 SMTP ERROR: QUIT command failed:
SMTP Error: Could not connect to SMTP host.
Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: Could not connect to SMTP host. in C:\xampp\htdocs\PHPMailer\vendor\phpmailer\phpmailer\src\PHPMailer.php:2136 Stack trace: #0 C:\xampp\htdocs\PHPMailer\vendor\phpmailer\phpmailer\src\PHPMailer.php(1960): PHPMailer\PHPMailer\PHPMailer->smtpConnect(Array) #1 C:\xampp\htdocs\PHPMailer\vendor\phpmailer\phpmailer\src\PHPMailer.php(1638): PHPMailer\PHPMailer\PHPMailer->smtpSend('Date: Wed, 30 J...', 'This is the HTM...') #2 C:\xampp\htdocs\PHPMailer\vendor\phpmailer\phpmailer\src\PHPMailer.php(1471): PHPMailer\PHPMailer\PHPMailer->postSend() #3 C:\xampp\htdocs\PHPMailer\index.php(37): PHPMailer\PHPMailer\PHPMailer->send() #4 {main} thrown in C:\xampp\htdocs\PHPMailer\vendor\phpmailer\phpmailer\src\PHPMailer.php on line 2136
我正在使用以下 PHP 函数来执行此操作:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
if(isset($_POST['submit'])){
// Load Composer's autoloader
require 'vendor/autoload.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
//Server settings
$mail->SMTPDebug = 2; // Enable/disable verbose debug output, change this to 2 if you want to see it doing its thing :)
$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 = 'test@gmail.com'; // SMTP username
$mail->Password = '123456abc'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 587; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
$mail->setFrom('no-reply@example.com', 'Alex'); // From address
$mail->addAddress('test@gmail.com'); // Add a recipient, In this case your email address
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Testing Email';
// $mail->Body = 'Name: ' .$_POST['name']. '<br>Email: ' .$_POST['email']. '<br>Message: ' .$_POST['message'];
$mail->Body = 'This is the HTML body';
$mail->send();
echo '<h2 style="color:red">Thank you '.$_POST['name'].', your message has been sent successfully</h2>';
这是我的在线 PHP 编辑器:https://paiza.io/projects/D-J1uBPeHJuL8jCOhToRQQ
这是我的文件路径:
我尝试过的:
-
我已经按照这个视频链接https://www.youtube.com/watch?v=oKbr2lRD7lQ一步步创建了一个电子邮件功能。但也无法将邮件发送到所选的电子邮件地址。
-
安全性较低的应用访问已开启。
希望有人可以指导我或指出我错在哪一部分。非常感谢。
【问题讨论】:
-
这能回答你的问题吗? how to send mail using phpmailer on xampp
-
我已尝试按照您建议的链接进行操作。也不能工作