繁体   English   中英

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

[英]How do i suppress errors using PHPMailer (mail is sent and received successfully in spite of error msg)?

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

<?php
  
  use PHPMailer\PHPMailer\PHPMailer;
  use PHPMailer\PHPMailer\Exception;
  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 客户端 -> 服务器:EHLO 本地主机 2022-06-09 08:34:53 客户端 -> 服务器:STARTTLS 2022-06-09 08:34:54 客户端 -> 服务器: EHLO localhost 2022-06-09 08:34:54 客户端-> 服务器:身份验证登录 2022-06-09 08:34:54 客户端-> 服务器:[隐藏凭据] 2022-06-09 08:34:55 客户端- > 服务器:[隐藏凭据] 2022-06-09 08:34:55 客户 -> 服务器:邮件来自:2022-06-09 08:34:55 客户 -> 服务器:RCPT 至:2022-06-09 08: 34:56 客户端 -> 服务器:数据 2022-06-09 08:34:56 客户端 -> 服务器:日期:2022 年 6 月 9 日星期四 08:34:53 +0000 2022-06-09 08:34:56 客户端 - > 服务器:至:xxx@xxx.com 2022-06-09 08:34:56 客户 -> 服务器:来自:xxxxx@gmail.com 2022-06-09 08:34:56 客户 -> 服务器:主题:表格标题:测试 2022-06-09 08:34:56 CLIENT -> SERVER:Message-ID:8tGuy8dJRkaxDXE6ocPdUX6bz3uyqNuOPNTSIVOsM@localhost 2022-06-09 08:34:56 CLIENT -> SERVER:X-Mailer:PHPMailer 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 -> SERVER: Content-Type: text/html; charset=UTF-8 2022-06-09 08:34:56 客户端 -> 服务器:2022-06-09 08:34:56 客户端 -> 服务器:来自:测试 2022-06-09 08:34:56 客户端 - > 服务器:电子邮件:test@gmail.com 2022-06-09 08:34:56 客户端 -> 服务器:消息:测试 2022-06-09 08:34:56 客户端 -> 服务器:2022-06-09 08: 34:56 客户端-> 服务器:。 2022-06-09 08:34:57 CLIENT -> SERVER: QUIT 电子邮件发送成功

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

既然邮件是通过的,有没有办法抑制这个错误信息?

编辑:正如 droopsnoot 指出的,将 smtpdebug 设置为 0 会降低错误的准确性,但我仍然收到以下“错误:电子邮件发送成功”

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

$mail->SMTPDebug = 1;

Email sent successfully是您自己的代码!

echo "Email sent successfully";

如果您不希望它显示,也请删除该行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM