Welcome Guest, Not a member yet? Register   Sign In
[Solved] Codeigniter: Load Library in a Core Class
#11

I would say that in the most cases it won't make sense and it is more advisable to only send an email in case of some particular processes.

In this specific project I want to receive an email in case of an error (no information or debug message) on the production environment. The reason for that is that I do not want to monitor the project all the time. Thus, I adjusted the above posted code slightly:

PHP Code:
class MY_Log extends CI_Log {

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

 
 public function write_log($level$msg)
 
 {
 
   $result parent::write_log($level$msg);

 
   //SEND MAIL
 
   if($level == 'error' && ENVIRONMENT == 'production')
 
   {
 
     require_once(BASE_PATH.'application/libraries/PHPMailer/PHPMailerAutoload.php');
 
     
      $mail 
= new PHPMailer;
 
     $mail->CharSet "UTF-8";
 
     $mail->isSMTP();
 
     $mail->Subject $level;
      $mail->Body $msg;
 
     //additional email stuff...

 
     $mail->send();
 
   }
 
   //END SEND MAIL

 
   return is_int($result);
 
 }



Thank you for your input
Reply
#12

I did find one way to incorporate the CI email class. It uses the same basic approach you have used in loading PHPMailer.  This even loads and uses application/config/email.php . (It is assumed to be there which is dangerous but this is just a proof-of-concept.)

Code:
class MY_Log extends CI_Log
{
    protected $mail;

    public function __construct($config = array())
    {
        parent::__construct($config);
        
        require(APPPATH.'config/email.php'); // adds $config to this scope

        require_once(BASEPATH.'libraries/Email.php');
        $this->mail = new CI_Email($config);
    }

    public function write_log($level, $msg)
    {
        //log_message($level, $msg);
        parent::write_log($level, $msg);

        if($level == 'error' && ENVIRONMENT == 'production')
        {
                      //do email stuff
        }
    }

}

For this to work there is one small hack that MUST BE MADE to system/libraries/Email.php

The last line in the constructor must be removed (or commented out)

Code:
   //log_message('info', 'Email Class Initialized');

Otherwise a loop condition exists where that call (log_message) tries to load the Log class, which tries to instantiate the Email class and... around and around we go.

So, there's your CI email class. Hope you don't want to load any other core libraries.
Reply
#13

(11-03-2016, 06:44 PM)dave friend Wrote: So, there's your CI email class. Hope you don't want to load any other core libraries.

I hope not Wink Thank you for all your input.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB