无法使用 XAMPP 发送电子邮件
Posted
技术标签:
【中文标题】无法使用 XAMPP 发送电子邮件【英文标题】:Cannot send the email through using XAMPP 【发布时间】: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
这是我的文件路径:
filepath
我尝试过的:
我已经按照这个视频链接https://www.youtube.com/watch?v=oKbr2lRD7lQ一步步创建了一个电子邮件功能。但也无法将邮件发送到所选的电子邮件地址。
安全性较低的应用访问已开启。
希望有人可以指导我或指出我错在哪一部分。非常感谢。
【问题讨论】:
这里已经回答了***.com/a/46237342/10806531 这能回答你的问题吗? how to send mail using phpmailer on xampp 我已尝试按照您建议的链接进行操作。也不能工作 【参考方案1】:从您的配置来看,它看起来很合适。请尝试使用端口 465
【讨论】:
致命错误:C:\xampp\htdocs\PHPMailer\vendor\phpmailer\phpmailer\src\SMTP.php 1228行的最大执行时间超过120秒错误出来【参考方案2】:从https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps字面上复制
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "username@gmail.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//Set who the message is to be sent from
$mail->setFrom('from@example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto@example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto@example.com', 'John Doe');
【讨论】:
以上是关于无法使用 XAMPP 发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章
SendMail 错误:无法使用 gmail 帐户发送电子邮件? XAMPP