Reputation: 3015
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 = '[email protected]';
$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
Upvotes: 0
Views: 1725
Reputation: 172
I was able to overcome this in one of my Lib
classes with Cake 3 using the following...
$oRegistry = new ComponentRegistry();
$oComponent = $oRegistry->load('PluginName.ComponentName');
I'm unsure if this creates any kinda discord with the state with the app's component registry; but, for my purposes, it seems to work.
Hope this helps someone :)
Upvotes: 0
Reputation: 60453
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('[email protected]')
->to($toEmail)
->subject($subject)
->send();
Upvotes: 2
Reputation: 126
$this can be use in class for their on variables and method define within class In cakephp $this is used in controller when the $components array is defined within controller.
Try this
$email = EmailComponent();
$email->$smtpOptions= array();
Upvotes: 0