2013-11-25 21 views
0

我正在cakephp 2.3上工作。我想將電子郵件組件加載到我的Lib文件夾中的類中。對於供應商相同的情況下,在那一刻文件了我在做什麼是這個..如何在Lib或Vendor文件中加載組件

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(); 



    } 

我不能夠使用$this ..我得到這個錯誤

$這個時候不是在對象上下文

回答

2

你不這樣做,組件控制器「擴展」,他們不包換沒有他們使用。

對於電子郵件用途,請使用CakeEmail class(反正電子郵件組件已棄用)。

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(); 
+0

thaknyou ..我在我的Lib文件夾中的類中使用這個嗎? – hellosheikh

+0

@hellosheikh您可以在任何需要的地方使用CakeEmail類,所以可以在庫中使用它。 – ndm

+0

thankyouuuuuu它的工作原理 – hellosheikh

0

$這可以在類中使用它們的變量和方法在類中定義 在cakephp $中,當$ components數組在控制器中定義時,它被用在控制器中。

試試這個

$email = EmailComponent(); 
$email->$smtpOptions= array(); 
+0

謝謝你們answer.error來了..這是「調用未定義功能EmailComponent();」 – hellosheikh

0

我可以用下面的蛋糕3戰勝這在我Lib類之一...

$oRegistry = new ComponentRegistry(); 
$oComponent = $oRegistry->load('PluginName.ComponentName'); 

我不能確定這是否造成任何還挺與國家不和諧應用的組件註冊表;但是,爲了我的目的,它似乎工作。

希望這有助於使用您的代碼後,有人:)

相關問題