In CodeIgniter, How Can I Have PHP Error Messages Emailed to Me?

Ian Cook picture Ian Cook · Nov 4, 2008 · Viewed 8k times · Source

I'd like to receive error logs via email. For example, if a Warning-level error message should occur, I'd like to get an email about it.

How can I get that working in CodeIgniter?

Answer

Adam picture Adam · Nov 4, 2008

You could extend the Exception core class to do it.

Might have to adjust the reference to CI's email class, not sure if you can instantiate it from a library like this. I don't use CI's email class myself, I've been using the Swift Mailer library. But this should get you on the right path.

Make a file MY_Exceptions.php and place it in /application/libraries/ (Or in /application/core/ for CI 2)

class MY_Exceptions extends CI_Exceptions {

    function __construct()
    {
        parent::__construct();
    }

    function log_exception($severity, $message, $filepath, $line)

    {   
        if (ENVIRONMENT === 'production') {
            $ci =& get_instance();

            $ci->load->library('email');
            $ci->email->from('[email protected]', 'Your Name');
            $ci->email->to('[email protected]');
            $ci->email->cc('[email protected]');
            $ci->email->bcc('[email protected]');
            $ci->email->subject('error');
            $ci->email->message('Severity: '.$severity.'  --> '.$message. ' '.$filepath.' '.$line);
            $ci->email->send();
        }


        parent::log_exception($severity, $message, $filepath, $line);
    }

}