CakePHP电子邮件配置 [英] CakePHP email configuration

查看:129
本文介绍了CakePHP电子邮件配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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:

And my 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',
    );

}

当我输入用户名和电子邮件时,以下错误:

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

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

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

有人可以帮助我吗?

推荐答案

只是您需要使用一种配置:

just you need use one configuration:

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

或者在您的情况下:

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

或者如果您要使用gmail:

or if you want to user a 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',
    );*/

}

这篇关于CakePHP电子邮件配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆