Welcome Guest, Not a member yet? Register   Sign In
email.php config file not loaded if CI_Email class is extended
#1

[eluser]Unknown[/eluser]
Hi everyone

I'm having a weird problem with CI. I'm trying to extend the Email class and, at the same time, configure some of the Email class parameters in a email.php config file. Here's the basic code:

Code:
class GS_Email extends CI_Email {

public function __construct () {
  parent::__construct();
  $this->from(
   $CI->config->item('gs_email_from_email'),
   $CI->config->item('gs_email_from_name')
  );
}

function send_activation_email ($activation_link) {
  $CI =& get_instance();
  $this->subject($CI->config->item('gs_user_activation_email_subject'));
  $body = str_replace(
   '[[[ACTIVATION_LINK]]]',
   $activation_link,
   $CI->config->item('gs_user_activation_email_body')
  );
  $this->message($body);
  return $this->send();
}
}

The problem with it is that the config file is not automatically loaded when I use the class. The only way I found to get it to work is to include 'email' in the autoload (which should not be necessary, right?) and initialize the Email class this way:

Code:
public function __construct () {
  parent::__construct();
  $CI =& get_instance();
  $this->initialize($CI->config->config);
  $this->from(
   $CI->config->item('gs_email_from_email'),
   $CI->config->item('gs_email_from_name')
  );
}

Is this a bug? Also, is there a way to avoid using $CI =& get_instance(); inside every single function?

Thank you so much!
#2

[eluser]Unknown[/eluser]
I just came across extending the Email class with the similar problems.
It turns out that you will need to pass the parameters to the parent constructor.

So the code will look like:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Email extends CI_Email
{

    public function __construct($config = array())
    {
        parent::__construct($config);
    }


}

?>

Since the CI_Email __construct takes in the $config array as a parameter.
Code:
/**
  * Constructor - Sets Email Preferences
  *
  * The constructor can be passed an array of config values
  */
public function __construct($config = array())
{
  if (count($config) > 0)
  {
   $this->initialize($config);
  }
  else
  {
   $this->_smtp_auth = ($this->smtp_user == '' AND $this->smtp_pass == '') ? FALSE : TRUE;
   $this->_safe_mode = ((boolean)@ini_get("safe_mode") === FALSE) ? FALSE : TRUE;
  }

  log_message('debug', "Email Class Initialized");
}




Theme © iAndrew 2016 - Forum software by © MyBB