Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Extending email class
  • Hi Guys! I'm trying to extend the email library to change the message method. Here is my code http://scrp.at/5j For some reason the method in the extended class never gets called. Controller
      $data = array();
                    $data['username'] = $user->username;
                    $data['password'] = $user->password;
                    Email::factory()
                            ->subject('test sub')
                            ->from('abc@yahoo.com') 
                            ->to('abc@yahoo.com')
                            ->message('user/passwordreminder.stags',$data);
                            ->send(); 
                    Email::print_debugger();
    

    The class file is in the app/classes/email.php Any idea?
  • Code updated http://scrp.at/5l also added 'Email' => APPPATH.'classes/email.php', to the bootloader as suggested by huglester still not working. The method never gets called.
  • Can anyone help?
  • From where do you call the Email class? And how do you call it? And your app's bootstrap.php contains this?
    Autoloader::add_classes(array(
     'Email' => APPPATH.'classes/email.php',
    ));
    
  • Harro Verton wrote on Monday 27th of June 2011:
    From where do you call the Email class? And how do you call it? And your app's bootstrap.php contains this?
    Autoloader::add_classes(array(
     'Email' => APPPATH.'classes/email.php',
    ));
    

    Yes, I've added the class to the bootstrap.php as you have shown exactly. I'm calling the it from the controller (app/classes/controller/users.php) using the code below:
    $data = array();
     $data['username'] = $user->username; 
    $data['password'] = $user->password; 
    Email::factory() ->subject('test sub')
                               ->from('abc@yahoo.com')
                               ->to('abc@yahoo.com')
                               ->message('user/passwordreminder.stags',$data)
                              ->send(); 
            Email::print_debugger();
    
  • The problem here is that the Email class is a static 'front' for Email functionality. When you call Email::factory(), you'll get a driver object returned based on the type of mail transport you have configured. Your code then uses this object to construct the message. The original Email class isn't used anywhere here, other then for the factory call. You can check this using
    $email = Email::factory();
    Debug::dump($email);
    
    You'll see you get a email driver instance returned.
  • So would you extend or create a new email driver?
  • what would be the best solution for these type of problem?
  • Depends. If it's a minor change, I would extend it. If it's functionally different, create a new driver. In this case, I would probably do both (class Email_FromTemplate extends \Fuel\Fore\Email_Sendmail for example), and then define 'FromTemplate' as the protocol in your app's config/email.php. It clearly indicates it's new functionality, but you still reuse as much as possible.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion