2012-07-05 20 views
0

我嘗試CakeEmail和我有問題。 我將代碼添加到了我的add函數中,類似於博客教程的add函數,但是當我添加一個項目時,它表示未定義的變量。如果我刪除了電子郵件代碼,它會起作用,如果我將它放入並刷新頁面中顯示的項目,但不會發送電子郵件。CakeEmail不能在添加功能

這裏是從功能的代碼:

public function add() 
    { 
     $this->set('isAddValid', false); 
     $this->set('addValidationErrors', false); 

     if ($this->request->is('post')) 
     { 
      // If the save is successful, close the dialog and display the success message 
      // Else, set the error flags. 
      if ($this->LocalClock->save($this->request->data)) 
      { 
       $this->set('localClocks', $this->request->data); 
       $this->LocalClock->save($localClocks); 
       $this->set('isAddValid', true); 
       $this->set('addValidationErrors', false); 

       $email = new CakeEmail('smtp'); 
       $email->from(array('[email protected]' => 'Local Clocks')); 
       $email->to('[email protected]'); 
       $email->subject('New Local Clock Added'); 
       $email->send('A new local clock has been added.'); 
       CakeEmail::deliver('[email protected]', 'New local Clock', 'New Local Clock Added', 
        array('from' => '[email protected]')); 
      } 
     } 

下面是在配置文件夾中的文件email.php(我只是複製從他們在那裏的默認文件的貼吧):

class EmailConfig { 
    public $default = array(
     'transport' => 'Mail', 
     'from' => '[email protected]', 
     //'charset' => 'utf-8', 
     //'headerCharset' => 'utf-8', 
    ); 

    public $smtp = array(
     'transport' => 'Smtp', 
     'from' => array('[email protected]' => 'My Site'), 
     'host' => 'localhost', 
     'port' => 25, 
     'timeout' => 30, 
     'username' => 'user', 
     'password' => 'secret', 
     'client' => null, 
     'log' => false, 
     //'charset' => 'utf-8', 
     //'headerCharset' => 'utf-8', 
    ); 

    public $fast = array(
     'from' => '[email protected]', 
     'sender' => null, 
     'to' => null, 
     'cc' => null, 
     'bcc' => null, 
     'replyTo' => null, 
     'readReceipt' => null, 
     'returnPath' => null, 
     'messageId' => true, 
     'subject' => null, 
     'message' => null, 
     'headers' => null, 
     'viewRender' => null, 
     'template' => false, 
     'layout' => false, 
     'viewVars' => null, 
     'attachments' => null, 
     'emailFormat' => null, 
     'transport' => 'Smtp', 
     'host' => 'localhost', 
     'port' => 25, 
     'timeout' => 30, 
     'username' => 'user', 
     'password' => 'secret', 
     'client' => null, 
     'log' => true, 
     //'charset' => 'utf-8', 
     //'headerCharset' => 'utf-8', 
    ); 
} 

任何幫助,將不勝感激

感謝

+1

你歌廳哪些錯誤? – Dgo 2012-07-05 13:24:22

+0

我收到通知(8):未定義的變量:localClocks, 奇怪的是它指定的錯誤在任何電子郵件代碼之前。 – RXC 2012-07-05 13:26:12

+1

$ this-> Email - > _ debug = true;放入添加方法,並...看到你得到錯誤。 – Dgo 2012-07-05 13:30:55

回答

1

我發現答案要求我添加另一個設置到email.php文件。

我創建了一個Gmail帳戶,以便我可以使用電子郵件功能。

的代碼非常簡單:

public $gmail = array(
     'host' => 'ssl://smtp.gmail.com', 
     'port' => 465, 
     'username' => '[email protected]', 
     'password' => 'password', 
     'transport' => 'Smtp', 
     'timeout' => 1 
    ); 

並在Add()函數:

$email = new CakeEmail('gmail'); 
3

你的問題是前實際上錯誤說的是 - 「未定義變量:localClocks」。

$this->set('localClocks', $this->request->data); 
$this->LocalClock->save($localClocks); 

這不設置一個變量$ localClocks - 它傳遞數據給一個變量,這將是從視圖訪問(但不可訪問控制器)。

切換到這一點,你應該罰款(或至少讓過去這個問題):

$this->LocalClock->save($this->request->data); 

或者,如果你真的想將它分解成2行:

$localClocks = $this->request->data; 
$this->LocalClock->save($localClocks); 

如果你願意,你仍然可以「設置」在視圖中使用的變量,但是 - 使用$ this-> set不會使控制器可訪問變量。

+0

謝謝你的收穫。我實際上刪除了那行代碼,因爲我不需要它。我不知道爲什麼我把它放在那裏,因爲它已經保存了數據。至於電子郵件,它仍然不會發送任何東西。我只是得到一個空白的屏幕。 – RXC 2012-07-05 13:46:18

+1

可能是一個好主意,可以進行一些調試以檢查是否真的進入了if塊,並在實際發送電子郵件的同時包裝if()。 – Dave 2012-07-05 14:17:36

+0

我認爲我的問題是我實際上沒有設置電子郵件客戶端。我會繼續嘗試,謝謝。 – RXC 2012-07-05 15:03:56