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.
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 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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With