Codeigniter Email error handling

后端 未结 3 1148
[愿得一人]
[愿得一人] 2020-12-18 18:54

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.

3条回答
  •  臣服心动
    2020-12-18 19:57

    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:

    _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.

    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.

提交回复
热议问题