2014-05-19 2 views
1

사용자가 암호를 잊어 버렸을 때 CakePHP 전자 메일을 사용하여 전자 메일을 새 암호로 보내고 싶습니다. 사용자가 비밀번호를 잊어 버린 경우 사용자 이름과 해당 이메일을 입력해야합니다. 시스템은 사용자 이름과 전자 메일이 데이터베이스에서 일치하는지 확인하고 일치하는 경우 임의의 암호를 만들어 사용자 전자 메일 계정으로 보냅니다. 나는 초보자이고 제대로하고 있는지 확실하지 않습니다. 다음과 같이 내 코드는 다음과 같습니다CakePHP 전자 메일 구성

employeesController :

<?php 
App::uses('AppController', 'Controller'); 
App::uses('SimplePasswordHasher', 'Controller/Component/Auth'); 
App::uses('CakeEmail', 'Network/Email'); 

class EmployeesController extends AppController { 

//some code 

public function forgotpassword() 
    { 
     $username=$this->request->data['fusername']; 
     //App::uses('SimplePasswordHasher', 'Controller/Component/Auth'); 
     //$passwordHasher = new SimplePasswordHasher(); 
     //$password = $passwordHasher->hash($this->request->data['password']); 
     $emailaddress=$this->request->data['emailadd']; 
     $msg = $this->Employee->getUserPassword($username,$emailaddress); 
     if($msg) 
     { 

      foreach ($msg as $userdetails) 
      { 
       $userhashedpass=$userdetails['Employee']['employee_pw'];//see access level here 
       $userid=$userdetails['Employee']['id']; 
       $useremail=$userdetails['Employee']['employee_email']; 


      } 
      $newpassword="$userhashedpass[0]$userhashedpass[4]$userhashedpass[13]$userhashedpass[8]$userhashedpass[11]$userhashedpass[12]"; 

      //send email 

      $Email = new CakeEmail(); 
      $Email->from(array('[email protected]' => 'CentreVision CRM')) 
       ->to($useremail) 
       ->subject('New Password') 
       ->send('Your new password is $newpassword '); 

      /* $to = $useremail; 
      $subject = "Your New Password"; 
      $txt = "Your new password is $newpassword "; 
      $headers = "From: [email protected]" . "\r\n"; 
*/ 
//send email to the employee 


      // $reply=$this->Employee->updatenewpassword($username,$emailaddress,$newpassword); 
      $data = array('id' => $userid, 'employee_pw' => $newpassword); 
      // This will update Recipe with id 10 
      $this->Employee->save($data); 
      //$this->set('msg',$userhashedpass); 
      //$this->set('newpassword',$newpassword); 
      $errormsg="Email has been sent to registered email address linked to this username"; 
      //comment out next line to not display the new password on the screen once smtp is configured 
      $this->set('newpassword',$newpassword); 
      $this->set('errorfor',$errormsg); 
      $this->render("../Pages/home"); 
      $this->layout = '../Pages/home'; 
     } 
     else{ 
      $errormsg="Username and Email does not match"; 
      $this->set('errorfor',$errormsg); 
      $this->render("../Pages/home"); 
      $this->layout = '../Pages/home'; 
     } 

    } 

//some code 

} 

그리고 내 응용 프로그램/설정/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', 
    ); 

} 

순간 나는 사용자 이름을 입력하고 난 다음과 같은 오류가 이메일을 보내 :

메일() : " 로컬 호스트 " 포트 25에서 메일 서버에 연결하는 데 실패, 당신의 " SMTP를 확인 " 및 " smtp_port " php.ini에서 설정하거나 ini_set()을 사용하십시오.

나를 도울 수있는 사람이 있습니까?

답변

5

는 하나 개의 구성을 사용할 필요

$Email = new CakeEmail(); 
$Email->config('default'); 

또는 귀하의 경우

:

$Email = new CakeEmail(); 
$Email->config('smtp'); 

또는 사용자에게 Gmail을 원하는 경우 :

$Email = new CakeEmail(); 
$Email->config('gmail'); 

및 구성 :

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]' => 'Test Site'), 
     'host' => 'localhost', 
     'port' => 25, 
     'timeout' => 30, 
     'username' => 'user', 
     'password' => 'secret', 
     'client' => null, 
     'log' => false, 
     //'charset' => 'utf-8', 
     //'headerCharset' => 'utf-8',  
    );*/ 
    public $gmail = array(
     'host' => 'ssl://smtp.gmail.com', 
     'port' => 465, 
     'username' => '[email protected]', 
     'password' => 'password', 
     'transport' => 'Smtp' 
    ); 
    /*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', 
    );*/ 

} 
관련 문제