Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter Email error handling

Tags:

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.

like image 204
samxli Avatar asked Jun 19 '11 16:06

samxli


People also ask

How to show email error in CodeIgniter?

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.

How to do error reporting on in CodeIgniter?

CodeIgniter default environment is development and so error reporting by default in turn on state, if you want to turn off reporting then change the environment value(in the top of the main index. php) to production or testing. The above method will work with only For >= 2.

How to use email in CodeIgniter?

Sending an EmailThe subject() and message() function is used to set the subject and message of the email. $this->email->from('[email protected]', 'Your Name'); $this->email->to('[email protected]'); $this->email->subject('Email Test'); $this->email->message('Testing the email class.


2 Answers

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.

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_clear parameter of the email->send() function in order to not clear the message and the debugging, effectively killing the debugger function if you fail to pass the FALSE.

like image 86
hakre Avatar answered Oct 03 '22 21:10

hakre


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:

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

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.

like image 30
Francois Deschenes Avatar answered Oct 03 '22 21:10

Francois Deschenes