CodeIgniter Forums

Full Version: Problem with extended Email class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]maikens[/eluser]
I am in the process of trying to debug a problem in my application where users are being logged out unexpectedly. I took it as an opportunity to configure the email aspect of the app as well. What I am trying to do is set up my application so that whenever a session is destroyed, I'll get an email with a dump of the session userdata, and the debug_backtrace(). It's not the best method, but it's a start

My problem is that when I logout (and thus destroy the session), I get an error saying that the $email variable is undefined in the controller (even though I'd already autoloaded it).

The error is:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Dashboard::$email

Filename: libraries/MY_Session.php

Line Number: 43

MY_Session:

Code:
// ...

function sess_destroy() {
    parent::sess_destroy();
    ob_start();
    echo 'Userdata before destruction:';
    var_dump($this->userdata);
    echo 'Debug backtrace:';
    var_dump(debug_backtrace());
    $msg = ob_get_clean();
    $ci =& get_instance();
    $ci->email->send('[email protected]', '[email protected]', 'Session destroyed', $msg);
    $this->userdata = array();
}

// ...

MY_Email:

Code:
class MY_Email extends CI_Email {
  
  /**
   * Constructor
   */
  public function __construct($config) {
    parent::__construct($config);
    // todo: initialization, etc to go here
  }
  
  /**
   * Overrides the existing CI Email send function to perform some extra actions
   * before doing the actual send
   *
   * @param mixed $from a string, or an indexed or associative array. Can contain to and reply_to addresses/names
   * @param mixed $recipients a string or associative array. Can contain to, cc, and bcc addresses
   * @param string $subject the subject of the email
   * @param mixed $body a string or associative array. Can contain html and text email bodies
   * @param array $headers contains any email headers you wish to explicitly set
   * @param array $attachments contains paths to files to attach to the email
   * @return bool whether the email was successfully sent
   */
  public function send($from, $recipients, $subject, $body, $headers = array(), $attachments = array()) {
    $this->clear(); // clear any parameters from previous calls to send()
    
    // From and reply_to address
    if(is_array($from)) {
      if(isset($from['from'])) {
        $this->from($from['from'][0], $from['from'][1]);
        $this->reply_to($from['reply_to'][0], $from['reply_to'][1]);
      }
      else $this->from($from[0], $from[1]);
    }
    else $this->from($from);
    
    // Handle recipients (to, cc, bcc)
    if(is_array($recipients) && isset($recipients['to'])) {
      $this->to($recipients['to']);
      $this->cc($recipients['cc']);
      $this->bcc($recipients['bcc']);      
    }
    else $this->to($recipients);
    
    // Handle subject
    $this->subject($subject);
    
    // Handle setting html and text email bodies
    if(is_array($body)) {
      $html = $this->html_template($body['html'], $subject);
      $this->message($html);
      $this->set_alt_message(strip_tags($body['text']));
    }
    else $this->message(strip_tags($body)); // assume text-only email

    // Handle setting mail headers
    if(!empty($headers)) {
      foreach($headers as $key => $value) $this->set_header($key, $value);
    }
    
    // Handle attaching files
    if(!empty($attachments)) $this->attach_files($attachments);
    
    return parent::send();
  }

// ...

Anyone have any idea why this is happening?

El Forum

[eluser]jakelehner[/eluser]
Autoload is loading the email library for Controllers and Models which have access to the default CI instance. Since you're creating your own instance of CI under $ci, you'll need to call $ci->load to load the email library under that instance.

Aside from that, this doesn't sound like the most efficient way to troubleshoot the sessions expiring early. Troubleshooting sessions is tricky, but the email approach likely won't yield much.

Figure out if your session is expiring when the 'sess_time_to_update' timer is expiring. That's pretty common and usually means a cookie issue or something is mis-configured. To test this, set your update value really low (in a dev environment) to something like 5 seconds. Log in, and start refreshing the page. If you're logged out in 5 seconds, you'll get a better idea of where to look.

El Forum

[eluser]Aken[/eluser]
[quote author="jakelehner" date="1344367933"]Autoload is loading the email library for Controllers and Models which have access to the default CI instance. Since you're creating your own instance of CI under $ci, you'll need to call $ci->load to load the email library under that instance.[/quote]

This isn't true. CI is loaded by reference, hence the get_instance() function (which, by the way, you don't need when extending the Session library, because there is already a $this->CI property available). The object will be the same, whether it's in your controller or a referenced object in a library.

I did a quick test and the Email property was available to me no problem. You should post more code or try and simplify your code down to where it works, and see what might've gone wrong after that.

El Forum

[eluser]maikens[/eluser]
Thanks for responding, jakelehner and Aken.

I tried using $this->CI->email, but I got the same error. As it turns out, I did try and load the email library again, which got me further along, but I hit another error. That error was due to my using a port that my hosting provider does not allow me to send email through. The next thing is determining why I can't send to certain domains through port 25 (probably because they;re blocked because of spam), and which port I should use.

Jake, thanks for your suggestion. I tried setting the update time to 5 seconds, but I was not logged out after refreshing after five seconds. I've scoured the internet about this problem, and mine seems to be fairly unique - it seems I've created a problem in how I handle checking sessions. I am going to wait until I am logged out randomly again, and see what is in the email.

And hey, if you can think of any other reasons why sessions are being destroyed randomly, in a general context, throw them my way- I can use some ideas at this point!

thanks again!

El Forum

[eluser]CroNiX[/eluser]
There are many threads dealing with sessions being lost as it's not an uncommon problem.