我如何使用PHPMailer抑制错误(邮件发送和接收成功,尽管错误消息)?



我使用以下代码发送邮件:

<?php

use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;
require 'vendor/autoload.php';
$receiving_email_address = 'somebody@somedomain.com';

$mail = new PHPMailer();
$mail->IsSMTP(); 
$mail->CharSet="UTF-8";
$mail->Host = "smtp.gmail.com";
$mail->SMTPDebug = 1; 
$mail->Port = 587 ; //465 or 587
$mail->SMTPSecure = 'tls';  
$mail->SMTPAuth = true; 
$mail->IsHTML(true);
//Authentication
$mail->Username = "someaddress@gmail.com";
$mail->Password = "********";

$msg = "From: ".$_POST['name']."nEmail: ".$_POST['email']."nMessage: ".$_POST['message'];
$msg = nl2br($msg);
//Set Params
$mail->SetFrom("sinhat@gmail.com");
$mail->AddAddress("tushar+forms@bluetide.co");
$mail->Subject = "Form with title: ".$_POST['subject'];
$mail->Body = $msg;


if(!$mail->Send()) {
echo "Error while sending Email.";
var_dump($mail);
} else {
echo "Email sent successfully";
}  
?>

这是从下面的HTML代码调用的:

<form action="contact.php" method="post" role="form" class="php-email-form">
<div class="row">
<div class="form-group col-md-6">
<label for="name">Your Name</label>
input type="text" name="name" class="form-control" id="name" required>
</div>
<div class="form-group col-md-6 mt-3 mt-md-0">
<label for="name">Your Email</label>
<input type="email" class="form-control" name="email" id="email" required>
</div>
</div>
<div class="form-group mt-3">
<label for="name">Subject</label>
<input type="text" class="form-control" name="subject" id="subject" required>
</div>
<div class="form-group mt-3">
<label for="name">Message</label>
<textarea class="form-control" name="message" rows="10" required></textarea>
</div>
<div class="my-3">
<div class="loading">Loading</div>
<div class="error-message">An error occured but your msg has been sent</div>
<div class="sent-message">Your message has been sent. Thank you!</div>
</div>
<div class="text-center"><button type="submit">Send Message</button></div>
</form>

邮件正在通过,但我的网页上出现了这个奇怪的错误代码块(截图也附在附件中):

错误:2022-06-09 08:34:53 CLIENT ->服务器:EHLO localhost 2022-06-09 08:34:53 CLIENT ->服务器:STARTTLS 2022-06-09 08:34:54 CLIENT ->服务器:EHLO localhost 2022-06-09 08:34:54 CLIENT ->服务器:AUTH LOGIN 2022-06-09 08:34:54 CLIENT ->服务器:[凭证隐藏]2022-06-09 08:34:55 CLIENT ->服务器:[凭证隐藏]2022-06-09 08:34:55 CLIENT ->服务器:邮件来自:2022-06-09 08:34:55 CLIENT ->服务器:RCPT TO: 2022-06-09 08:34:56 CLIENT ->服务器:DATA 2022-06-09 08:34:56 CLIENT ->服务器:日期:星期四,2022年6月9日08:34:53 +0000 2022-06-09 08:34:56 CLIENT ->SERVER: To: xxx@xxx.com 2022-06-09 08:34:56 CLIENT ->SERVER: From: xxxxx@gmail.com 2022-06-09 08:34:56 CLIENT ->SERVER: Subject: Form with title: test 2022-06-09 08:34:56 CLIENT ->服务器:消息id: 8tGuy8dJRkaxDXE6ocPdUX6bz3uyqNuOPNTSIVOsM@localhost 2022-06-09 08:34:56 CLIENT ->SERVER: X-Mailer: phpmmailer 6.6.0 (https://github.com/PHPMailer/PHPMailer) 2022-06-09 08:34:56 CLIENT ->SERVER: MIME-Version: 1.0 2022-06-09 08:34:56 CLIENT ->服务器:内容类型:text/html;charset=UTF-8 2022-06-09 08:34:56 CLIENT ->服务器:2022-06-09 08:34:56 CLIENT ->SERVER: From: test2022-06-09 08:34:56 CLIENT ->服务器:邮箱:test@gmail.com2022-06-09 08:34:56 CLIENT ->SERVER: Message: test 2022-06-09 08:34:56 CLIENT ->服务器:2022-06-09 08:34:56 CLIENT ->服务器:。2022-06-09 08:34:57 CLIENT ->服务器:QUIT Email sent successfully

无论我是在本地PHP服务器上运行代码还是将其推送到heroku,都会发生这种情况。

既然邮件通过了,有没有办法抑制这个错误消息?

编辑:正如droopsnoot指出设置smtpdebug为0减少了错误的严重性,但我仍然得到以下"错误":电子邮件发送成功">

奇怪的是,如果启用调试输出,就会得到调试输出。删除这一行:

$mail->SMTPDebug = 1;

Email sent successfully输出是您自己的代码!

echo "Email sent successfully";

如果你不希望它显示这一行,也删除这一行。

最新更新