php 使用 PHPMailer 处理错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2386544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:16:39  来源:igfitidea点击:

Error handling with PHPMailer

phpphpmailer

提问by Stomped

I'm trying to use PHPMailer for a small project, but I'm a bit confused about error handling with this software. Hoping someone has experience with it. When I've set up an email and I use:

我正在尝试将 PHPMailer 用于一个小项目,但我对使用该软件的错误处理感到有些困惑。希望有人有这方面的经验。当我设置电子邮件并使用:

$result = $mail->Send();

if(!$result) {
    // There was an error
    // Do some error handling things here
} else {
    echo "Email successful";
}

Which works fine, more or less. The problem is when there's an error, PHPMailer also seems to echo the error out, so if there's a problem, it just sends that info directly to the browser, essentially breaking any error handling I"m trying to do.

哪个工作得很好,或多或少。问题是当出现错误时,PHPMailer 似乎也会回显错误,所以如果出现问题,它只会将该信息直接发送到浏览器,基本上破坏了我正在尝试做的任何错误处理。

Is there a way to silence these messages? Its not throwing an exception, its just printing out the error, which in my test case is:

有没有办法让这些消息静音?它没有抛出异常,它只是打印出错误,在我的测试用例中是:

invalid address: @invalid@email You must provide at least one recipient email address.

Its meant to be an error, but it should be residing in $mail->ErrorInfo; not being echo'd out by the software.

它意味着一个错误,但它应该驻留在 $mail->ErrorInfo; 中。没有被软件回显。

回答by Phil Rykoff

PHPMAiler uses Exceptions. Try to adopt the following code:

PHPMAiler 使用异常。尝试采用以下代码

require_once '../class.phpmailer.php';

$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch

try {
  $mail->AddReplyTo('[email protected]', 'First Last');
  $mail->AddAddress('[email protected]', 'John Doe');
  $mail->SetFrom('[email protected]', 'First Last');
  $mail->AddReplyTo('[email protected]', 'First Last');
  $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
  $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
  $mail->MsgHTML(file_get_contents('contents.html'));
  $mail->AddAttachment('images/phpmailer.gif');      // attachment
  $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
  $mail->Send();
  echo "Message Sent OK\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}

回答by PhoneixS

You can get more info about the error with the method $mail->ErrorInfo. For example:

您可以使用方法获取有关错误的更多信息$mail->ErrorInfo。例如:

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

This is an alternative to the exception model that you need to active with new PHPMailer(true). But if can use exception model, use it as @Phil Rykoff answer.

这是您需要激活的异常模型的替代方法new PHPMailer(true)。但是如果可以使用异常模型,请将其用作@Phil Rykoff 的答案。

This comes from the main page of PHPMailer on github https://github.com/PHPMailer/PHPMailer.

这来自 github https://github.com/PHPMailer/PHPMailer上 PHPMailer 的主页。

回答by PhilMDev

Please note!!! You must use the following format when instantiating PHPMailer!

请注意!!!实例化 PHPMailer 时必须使用以下格式!

$mail = new PHPMailer(true);

If you don't exceptions are ignored and the only thing you'll get is an echo from the routine! I know this is well after this was created but hopefully it will help someone.

如果您不这样做,异常将被忽略,您将得到的唯一结果就是例程的回声!我知道这在创建之后很好,但希望它会帮助某人。

回答by Simon Roberts

Just had to fix this myself. The above answers don't seem to take into account the $mail->SMTPDebug = 0;option. It may not have been available when the question was first asked.

只好自己解决这个问题。上面的答案似乎没有考虑到这个 $mail->SMTPDebug = 0;选项。当第一次提出问题时,它可能不可用。

If you got your code from the PHPMail site, the default will be $mail->SMTPDebug = 2; // enables SMTP debug information (for testing)

如果您从 PHPMail 站点获得代码,则默认值为 $mail->SMTPDebug = 2; // enables SMTP debug information (for testing)

https://github.com/Synchro/PHPMailer/blob/master/examples/test_smtp_gmail_advanced.php

https://github.com/Synchro/PHPMailer/blob/master/examples/test_smtp_gmail_advanced.php

Set the value to 0 to suppress the errors and edit the 'catch' part of your code as explained above.

将值设置为 0 以抑制错误并编辑代码的“catch”部分,如上所述。

回答by Charlie Shehadi

We wrote a wrapper class that captures the buffer and converts the printed output to an exception. this lets us upgrade the phpmailer file without having to remember to comment out the echo statements each time we upgrade.

我们编写了一个包装类来捕获缓冲区并将打印输出转换为异常。这让我们可以升级 phpmailer 文件,而不必记住每次升级时都注释掉 echo 语句。

The wrapper class has methods something along the lines of:

包装器类具有以下方法:

public function AddAddress($email, $name = null) {
    ob_start();
    parent::AddAddress($email, $name);
    $error = ob_get_contents();
    ob_end_clean();
    if( !empty($error) ) {
        throw new Exception($error);
    }
}

回答by Fred

Even if you use exceptions, it still output errors.
You have to set $MailerDebug to False wich should look like this

即使使用异常,它仍然会输出错误。
你必须将 $MailerDebug 设置为 False ,它应该是这样的

$mail = new PHPMailer();
$mail->MailerDebug = false;

回答by Patdundee

This one works fine

这个很好用

use try { as above

use Catch as above but comment out the echo lines
} catch (phpmailerException $e) { 
//echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {   
//echo $e->getMessage(); //Boring error messages from anything else!
}

Then add this

然后添加这个

if ($e) {
//enter yor error message or redirect the user
} else {
//do something else 
}

回答by Haris ur Rehman

In PHPMailer.php, there are lines as below:

在 PHPMailer.php 中,有如下几行:

echo $e->getMessage()

Just comment these lines and you will be good to go.

只需评论这些行,您就可以开始了。

回答by Prince-W

$mail = new PHPMailer();

$mail->AddAddress($email); 
$mail->From     = $from;
$mail->Subject  = $subject; 
$mail->Body     = $body;

if($mail->Send()){
    echo 'Email Successfully Sent!';
}else{
    echo 'Email Sending Failed!';
}

the simplest way to handle email sending successful or failed...

处理电子邮件发送成功或失败的最简单方法...