几天后我在 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 逐步创建电子邮件功能。但也无法将邮件发送到选定的电子邮件地址。
安全性较低的应用访问已开启。
希望有人能指导我或指出我哪里出错了。非常感谢。
答案 0 :(得分:0)
如您所见,您的配置看起来不错。请尝试使用端口 465
答案 1 :(得分:0)
从字面上复制自 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');