Codeigniter Email error handling
Asked Answered
A

4

23

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.

Album answered 19/6, 2011 at 16:16 Comment(5)
Are you making use of the build-in CI error handling routines and settings?Wedekind
Possible Duplicate: #5777852Wedekind
If this is in production, I usually turn off all the error logging. Will the CI error handling log functions still work if I set the error reporting to none?Album
You should not turn off error logging in production, only error reporting: Disabling error reporting will NOT prevent log files from being written if there are errors. (CI error handling)Wedekind
The Email class logs it's owns errors internally regardless of whether errors are suppressed or not in a variable called $_debug_msg. I've included an answer for you which extends to functionality of the Email class and allows you to retrieve the array or debug messages.Tuning
W
20

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.

Wedekind answered 19/6, 2011 at 16:25 Comment(6)
I need the debugger output to a string since I am returning the errors to the front end (via json). Any way to do that?Album
I've edited the answer to show you how you could assign the debugger output to a variable as well.Wedekind
I have the send() in a loop for sending out mass emails. If the email is successful, I put a success message into the json. If failed, I can use the ob_end_clean() but are there side effects of using this since it will be in a loop?Album
I don't know your application, you have not showed nor told any of that in your question. So it's hard to tell. You can make $error an array so it's capable of containing more than one error message only. Or you concat the string. Added some code again for an example.Wedekind
@Album - Do you want to display the error in-between each message sent or at the end?Tuning
As of version 3.1.0 (maybe in previous versions too) print_debugger() already returns a string, not an output. So ob_start() and ob_get_clean() wrapping this function will get an empty string. You must directly do: $error = $this->email->print_debugger();Moist
T
9

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.

Tuning answered 19/6, 2011 at 16:53 Comment(3)
this solution looks pretty suitable since I am already overriding bits of the email class. i'll give it a try!Album
@Album - I've updated my example just in case you wanted to clear the debugger messages in-between attempts as you're looping through the e-mail addresses. I've added a clear_debugger_messages function and provided a detailed example. If you want to get all of the debugger messages after all the attempts are completed, simply don't clear them and you'll get a complete array at the end.Tuning
Are you placing this file in ./application/libraries or ./application/core ?Reception
T
2

Codeigniter 3:

if ( $this->email->send() ) {
    echo 'Your Email has successfully been sent.';
} else {

    $errors = $this->email->print_debugger();
    print_r($errors);
}
Timehonored answered 20/7, 2022 at 7:57 Comment(0)
T
-1

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.

Taranto answered 19/6, 2011 at 16:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.