PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, April 21, 2022

[FIXED] how to load a component in Lib or Vendor files

 April 21, 2022     cakephp, cakephp-2.0, cakephp-2.3     No comments   

Issue

i am working on a cakephp 2.3. i want to load Email component into my class which is in Lib folder. same case for Vendor files too at the moment what i am doing is this..

App::uses('EmailComponent', 'Controller/Component');
class Email { 

public static function sendemail($toEmail,$template,$subject) {
$this->Email->smtpOptions = array(
                            'port'=>'25',
                            'timeout'=>'30',

                            'host' => 'host',
                            'username'=>'username',
                            'password'=>'password'
                        );


        $this->Email->template = $template;
                        $this->Email->from    = 'no-reply@hello.com';
                        $this->Email->to      = $toEmail;
                        $this->Email->subject = $subject;
                        $this->Email->sendAs = 'both';

                        $this->Email->delivery = 'smtp';

                        $this->Email->send();



    }

i am not able to use $this.. i am getting this error

$this when not in object context


Solution

You don't do that, components are controller "extensions", they are not ment to be used without them.

For emailing purposes use the CakeEmail class (the E-Mail component is deprecated anyways).

App::uses('CakeEmail', 'Network/Email');

// ...

$Email = new CakeEmail(array(
    'port'      => 25,
    'timeout'   => 30,
    'host'      => 'host',
    'username'  => 'username',
    'password'  => 'password',
    'transport' => 'Smtp'
));

$Email->template($template)
      ->emailFormat('both')
      ->from('no-reply@hello.com')
      ->to($toEmail)
      ->subject($subject)
      ->send();


Answered By - ndm
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing