CakePHP电子邮件配置

时间:2014-05-19 12:12:26

标签: email cakephp

我想使用CakePHP Email在用户忘记密码时使用新密码向用户发送电子邮件。如果用户忘记了密码,则需要输入用户名和相应的电子邮件。系统将检查用户名和电子邮件是否在数据库中匹配,如果是,则创建随机密码并将其发送到用户电子邮件帐户。我是初学者,我不确定我是否正确地做到了。我的代码如下:

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('anuja_f@hotmail.com' => '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: admin@centervision.com" . "\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

}

我的app / config / email.php:

class EmailConfig {

    public $default = array(
        'transport' => 'Mail',
        'from' => 'anuja_f@hotmail.com',
        //'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',
    );

}

在我输入用户名和电子邮件的那一刻,我收到以下错误:

mail():无法连接到&#34; localhost&#34;的邮件服务器端口25,验证您的&#34; SMTP&#34;和&#34; smtp_port&#34;在php.ini中设置或使用ini_set()

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分: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' => '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' => 'guesss@gmail.com',
        'password' => 'password',
        'transport' => 'Smtp'
    );
    /*public $fast = array(
        'from' => 'contreras.amilkar@gmail.com',
        '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',
    );*/

}