2013-03-03 54 views
0

我有僱員應用程序,用戶可以辭職,活動,新建和轉移。我希望實現的是,當員工的職位從主動(standing_id = 1)更改/編輯時,應該能夠通過電子郵件發送,但不能發送。我在EmployeeController中放置了一個電子郵件功能。以下是我的代碼。當您在Cakephp中編輯字段時發送電子郵件

function _sendNewEmployeeEditMail($id) { 
$Employee = $this->Employee->read(null,$id); 
$email = new CakeEmail(); 
$email->from(array('[email protected]' => 'Testing App')); 
$email->to(array('[email protected]' => 'Name Surname')); 
$email->subject('New Employee'); 
$email->template('employee_email'); 
$email->viewVars(compact('Employee')); 
$email->emailFormat('html'); 
$edit_email = true;  
$current_status = $this->Employee->field('standing_id'); 
if($current_status==1) { 
$edit_email = false; 
if ($email->send()) { 
$this->Session->setFlash('Your employee has been submitted.','default',array('class' => 'notification')); 
return true; 
} else { 
$this->Session->setFlash('Your employee has not been submitted.','default',array('class' => 'error')); 
return false; 
} 
} 
} 

,並在我的編輯存儲功能這裏是我如何嘗試發送電子郵件

public function edit($id = null) { 
    $this->Employee->id = $id; 
    if (!$this->Employee->exists()) { 
     throw new NotFoundException(__('Invalid employee')); 
    } 
    if ($this->request->is('post') || $this->request->is('put')) { 
     if ($this->Employee->save($this->request->data)) { 
      $this->Session->setFlash(__('The employee has been saved'),'default',array('class' => 'notification')); 
      $this->_sendNewEmployeeEditMail($this->Employee->getLastInsertID() ); 
      $this->redirect(array('action' => 'index')); 
     } else { 
      $this->Session->setFlash(__('The employee could not be saved. Please, try again.'),'default',array('class' => 'error')); 
     } 
    } else { 
     $this->request->data = $this->Employee->read(null, $id); 
    } 
    $standings = $this->Employee->Standing->find('list'); 
    $this->set(compact('standings')); 
+1

你只是準備郵件在'_sendNewEmployeeEditMail'功能,但你永遠不會調用CakeEmail組件的'發送()'方法,所以這是可以預料的郵件永遠不會熄滅。 – Oldskool 2013-03-03 10:38:05

+0

@Oldskool我已經在send()方法中,並且仍然沒有收到任何電子郵件。任何想法可能導致這一點? – 2013-03-03 11:12:53

回答

0

您使用$this->Employee->getLastInsertID(),這隻會出現插入新員工後。

因爲您正在編輯現有員工,所以這將始終爲空。您應該使用$ id代替;

$this->_sendNewEmployeeEditMail($id); 
+0

即使我改變代碼 – 2013-03-03 16:16:56

+0

即使這可能無法解決問題,但我並不會贏,但這似乎是一個錯誤(insertid)。但是,你有沒有嘗試隔離這個問題?例如通過手動調用帶有固定ID的sendNewEmployeeEditMail()來發送電子郵件?檢查日誌文件? – thaJeztah 2013-03-03 16:22:05