0

我正在使用以下代码片段发送邮件,但移动到另一个主机搞砸了一切:

    public function forgetpwd(){    
//code omitted
    if($this->User->saveField('token_hash',$fu['User']['token_hash'] )){

                            //============Email================//
                            /* SMTP Options */
                            $this->Email->smtpOptions = array(
                                    'host' => 'smtp.gmail.com',                             
                                    'port'=>'465',
                                    'transport' => 'Smtp',
                                    'username'=>'email@gmail.com',
                                    'password'=>'secret',
                                    'tls' => true
                            );
                            $this->Email->template = 'resetpw';
                            $this->Email->from    = 'noreply@email.com';
                            $this->Email->to      = $fu['User']['name'].'<'.$fu['User']['email'].'>';
                            $this->Email->subject = __('Recover your password');
                            $this->Email->sendAs = 'both';

                            $this->Email->delivery = 'smtp';
                            $this->set('ms', $ms);
                            $this->Email->send();
                            $this->set('smtp_errors', $this->Email->smtpError);
                            $this->Session->setFlash(__('Check your email to recover your password'));
}

                            //============EndEmail=============//

所以有这个视图forgetpwd.ctp要求用户输入他/她的电子邮件地址,并且在尝试发送电子邮件时,我收到以下两个错误:

Error: The view for UsersController::forgetpwd() was not found.

Error: Confirm you have created the file: /home/public_html/development/app/View/Emails/text/resetpw.ctp

我确信resetpw.ctp存在于正确的位置。

我已经编辑了电子邮件配置并尝试采用 2.3 cakePHP 方式,这就是我最终得到的结果:

            if($this->User->saveField('token_hash',$fu['User']['token_hash'] )){

                App::uses('CakeEmail', 'Network/Email');

                $Email = new CakeEmail('default');

                $Email->to($fu['User']['email'])
                        ->subject('Reset your password')
                        ->message($key)
                        ->send();

                $this->Session->setFlash(__('Check your mail to reset your password'));

                $this->redirect(array('controller'=>'users','action'=>'reset'));

但是现在,当我尝试发送电子邮件时,我收到以下错误:

Call to a member function send() on a non-object
4

1 回答 1

0

改变

'host' => 'smtp.gmail.com',                             

'host' => 'ssl://smtp.gmail.com',

还将您的配置信息放在 App/Config/ 内的 email.php 中,如下所示:

http://book.cakephp.org/2.0/en/core-utility-libraries/email.html#configuration

于 2013-11-13T01:00:58.687 回答