php Codeigniter 电子邮件错误处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6403571/
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-26 00:16:22  来源:igfitidea点击:

Codeigniter Email error handling

phpemailcodeignitersmtp

提问by samxli

The CI Email send() function only returns true or false. Is there a way to get a more detailed reason as to why a sending failed? I'm using SMTP.

CI Email send() 函数只返回 true 或 false。有没有办法获得关于发送失败的更详细原因?我正在使用 SMTP。

回答by hakre

You can further inspect what happened by using the email debugger:

您可以使用电子邮件调试器进一步检查发生了什么:

$r = $this->send(FALSE);
if (!$r)
  $this->email->print_debugger()
  ;

From the Codeigniter Email Class Reference.

来自Codeigniter 电子邮件类参考

If you need the debugger output as a string, you can just catch the output with an output buffer:

如果您需要将调试器输出作为字符串,您可以使用输出缓冲区捕获输出

$errors = array();
... # Loop
$r = $this->send(FALSE);
if (!$r) {
  ob_start();
  $this->email->print_debugger();
  $error = ob_end_clean();
  $errors[] = $error;
}
... # Loop end

Codeigniter in more recent versions requires an explicit FALSE for the $auto_clearparameter of the email->send()functionin order to not clear the message and the debugging, effectively killing the debugger function if you fail to pass the FALSE.

较新版本的 Codeigniter 要求函数$auto_clear参数email->send()显式 FALSE ,以便不清除消息和调试,如果未能通过 FALSE,则有效地杀死调试器功能。

回答by Francois Deschenes

The print_debugger()function will work but it appends the e-mail header and message at the bottom. If all you want is an array of the debug message (which include both success and error messages), you could consider extending the functionality of the Email class as follows:

print_debugger()函数将起作用,但它会在底部附加电子邮件标题和消息。如果您只需要一个调试消息数组(包括成功和错误消息),您可以考虑扩展 Email 类的功能,如下所示:

<?php

class MY_Email extends CI_Email
{

  public function clear_debugger_messages()
  {
    $this->_debug_msg = array();
  }

  public function get_debugger_messages()
  {
    return $this->_debug_msg;
  }
}

You'd want to place this in a file named MY_Email.php in your ./application/libraries folder. CodeIgniter will automatically recognize the existence of this class and use it instead of it's default one.

您想将它放在 ./application/libraries 文件夹中名为 MY_Email.php 的文件中。CodeIgniter 会自动识别这个类的存在并使用它而不是它的默认类。

When you want to get a list (array) of debug messages, you can then do this:

当您想要获取调试消息列表(数组)时,您可以执行以下操作:

$this->email->get_debugger_messages();

If you're looping through messages and don't want to include debugger messages from previous attempts, you can do this:

如果您循环浏览消息并且不想包含以前尝试的调试器消息,您可以这样做:

foreach ($email_addresses as $email_address)
{
  $this->email->to($email_address);

  if (! $this->email->send())
  {
    echo 'Failed';

    // Loop through the debugger messages.
    foreach ($this->email->get_debugger_messages() as $debugger_message)
      echo $debugger_message;

    // Remove the debugger messages as they're not necessary for the next attempt.
    $this->email->clear_debugger_messages();
  }
  else
    echo 'Sent';
}

Reference: "Extending Native Libraries" section of https://www.codeigniter.com/user_guide/general/creating_libraries.html.

参考:https://www.codeigniter.com/user_guide/general/creating_libraries.html 的“扩展本地库”部分。

回答by martynthewolf

You could check your mail logs. If the mail errors out then you should have a record saying why in there.

你可以检查你的邮件日志。如果邮件出错,那么你应该有一个记录说明为什么在那里。

I'm not sure where they will be located though it depends on your system.

尽管这取决于您的系统,但我不确定它们将位于何处。