2012-03-09 41 views
0
private function sendmail($userid, $reportcontent,$email){ 
     //if($this->Session->read($this->_userName)) 
     { 
      $this->loadModel('EwtMailtemplate'); 
      $this->loadModel('EwtUser'); 
      $this->loadModel('EwtSetting'); 
      $this->autoRender = false; 

      $date = date("Y-m-d"); 
      $userinfo = $this->EwtUser->read(null, $userid); 
      $fullname = $userinfo['EwtUser']['fullname']; 
      $lastname = $userinfo['EwtUser']['lastname']; 
      $mailtempl = $userinfo['EwtUser']['mailtempl']; 
      if ($mailtempl == 0) { 
       $mailtempl = 1; 
      } 

      $setting = $this->EwtSetting->find('first'); 
      $mailhost = $setting['EwtSetting']['mailhost']; 
      $mailuser = $setting['EwtSetting']['mailuser']; 
      $mailpass = $setting['EwtSetting']['mailpass']; 
      //$reportmail = $setting['EwtSetting']['reportmail']; 
      $reportmail=$email; 
      $bodymail = $this->EwtMailtemplate->read(null, $mailtempl); 
      //$header = $bodymail['EwtMailtemplate']['header']; 
      //$footer = $bodymail['EwtMailtemplate']['footer']; 
      //$title = $bodymail['EwtMailtemplate']['title']; 
      $subject="New login password for working time system"; 
      //$subject = $lastname . " " . str_replace("[date]", $date, $title); 
      //$header = str_replace("[lastname]", $lastname, $header); 
      //$header = str_replace("[date]", $date, $header); 
      //$footer = str_replace("[lastname]", $lastname, $footer); 

      //$content = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><html><head><meta http-equiv="Content-Type" content="text/html; charset =utf-8" /></head><body>'.$header ."<br />" . $reportcontent . "<br />" . $footer . '</body></html>'; 

      $content = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"><html><head><meta http-equiv="Content-Type" content="text/html; charset =utf-8" /></head><body>'."<br />".$reportcontent."<br />".'</body></html>'; 

      $this->Email->to = $reportmail; 
      $this->Email->charset = 'UTF-8'; 
      $this->Email->from = sprintf("%s <%s>", $fullname, $mailuser); 
      $this->Email->replyto = sprintf("%s <%s>", $fullname, $mailuser); 
      $this->Email->subject = $subject; 
      $this->Email->sendAs ='html'; 
      $smtp = array(
         'port'=>25, 
         'host'=>$mailhost, 
         'timeout'=>99, 
         'username'=>$mailuser, 
         'password'=>$mailpass 
      ); 
      $this->Email->smtpOptions = $smtp; 
      $this->Email->delivery = 'smtp'; 

      $bool=($this->Email->send($content))?true:false; 

      $smtp_error = $this->Email->smtpError; 
      if (strlen($smtp_error)>0){ 
       //$this->Session->setflash($smtp_errors); 
       $bool=false; 
      } 
      if(!$bool) 
      { 
       $this->redirect(array('action'=>'userexists')); 
      } 
      return $bool; 
     } 
    } 

我使用該功能發送電子郵件到用戶提供的地址。按下按鈕後,會顯示一個視圖,我將此視圖稱爲newpassword(使用相同視圖名稱newpassword.ctp創建)。控制器的功能如下所示,視圖不按預期顯示

function newpassword() 
     { 
      $this->loadSkinForAction(); 
      $result=$this->EwtUser->get_user_from_email($_POST['email']); 

      if(!empty($result)) 
      { 
       $userid = $result[0]['ewt_users']['id']; 
       $password=$this->EwtUser->get_and_change_user_password($_POST['email']);    
       $mail="Your new password is: ".$password."<br/>Please use it for next login.<br/>You are recommended to change this password again in your 'Personal Profile' section."; 
       $bool = $this->sendmail($userid,$mail,$_POST['email']); 

      } 
      else 
      { 

       $this->redirect(array('action'=>'userexists')); 
      } 
     } 

而且形式使用挑起上述功能如下書面,

<div id="form_pwd" style="display:none;">  
      <form method="POST" action="/working_time/ewt_users/newpassword" id="new_pwd"> 
       <table> 
      <tr> 
       <td width="175px"><label for="email">Your email address</label></td>     
       <td><input type="text" name="email" id="email" size="35"/></td> 
      </tr> 
      <tr> 
       <td></td><td><input type="submit" value="Send" /></td> 
      </tr> 
       </table> 
      </form> 
     </div> 

我到現在麻煩的是,第一,大部分功能按照我的設想做,但在郵件發送後的最後一行,視圖(newpassword.ctp)根本不顯示。如果你們中的任何一個人能夠提供我所犯錯誤的地方,我真的很感激。非常感謝你。

+0

當你說視圖根本不顯示時,你的意思是輸出是空白的屏幕?我立即跳出來的是重定向到'sendmail()'方法中的'userexists'動作 - 嘗試用調試語句替換它,看看它是否正在執行該行。 – 2012-03-09 22:27:00

回答

0

您在設置$this->autoRenderfalse。我認爲這會爲當前進程設置它,從而使調用函數(newpassword())autoRender也失效。

我認爲你正在接近這個錯誤的方式恕我直言。

+0

我可能會添加,您在代碼中違反了很多cakephp約定。我建議花更多時間閱讀這本書。 – 2012-03-11 09:45:52