2
votes

I want to use CakePHP Email to send emails to users with new passwords when a user has forgotten their password. If the user has forgotten their password they will be required to enter their username and corresponding email. The system will check if the username and email match in the database and if so create a random password and send it to the users email account. I'm a beginner and I'm not sure if I'm doing it properly. My code is as follows:

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

}

And my app/config/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('site@localhost' => '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' => 'you@localhost',
        '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',
    );

}

At the moment when I enter username and email i get the following error:

mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()

Can someone please help me?

1

1 Answers

5
votes

just you need use one configuration:

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

or in your case:

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

or if you want to user a gmail:

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

and the configuration:

class EmailConfig {

    /*public $default = array(
        'transport' => 'Mail',
        'from' => 'you@localhost',
        //'charset' => 'utf-8',
        //'headerCharset' => 'utf-8',
    );

    public $smtp = array(
        'transport' => 'Smtp',
        'from' => array('soyazucar@localhost' => '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',
    );*/

}