【问题标题】:CakePHP email configurationCakePHP 电子邮件配置
【发布时间】:2014-07-07 09:30:08
【问题描述】:

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

员工控制器:

<?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(): 无法在“localhost”端口 25 连接到邮件服务器,请验证 php.ini 中的“SMTP”和“smtp_port”设置或使用 ini_set()

有人可以帮帮我吗?

【问题讨论】:

    标签: email cakephp


    【解决方案1】:

    只需要使用一种配置:

     $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',
        );*/
    
    }
    

    【讨论】:

      猜你喜欢
      • 2015-08-22
      • 2016-03-10
      • 2017-10-25
      • 2021-08-10
      • 1970-01-01
      • 1970-01-01
      • 2011-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多