从/app/Config/email.php获取会话

时间:2015-04-06 18:25:08

标签: session cakephp-2.0

我想使用CakeEmail发送电子邮件,但我需要从Session获取用户电子邮件和来自email.php的密码。我怎么能这样做?

email.php:

public $smtp = array(
            'host' => 'ssl://smtp.gmail.com',
            'port' => 465,
            'from' => array($_SESSION['UserLogged']['User']['email'] => $_SESSION['UserLogged']['User']['name']),
            'username' => $_SESSION['UserLogged']['User']['email'],
            'password' => $_SESSION['UserLogged']['User']['password'],
            'transport' => 'Smtp',
            'tls' => false // As of 2.3.0 you can also enable TLS SMTP
    );

我无法从Session中获取此值,因为它无法在email.php中读取,我无法从控制器设置密码...

2 个答案:

答案 0 :(得分:1)

您没有详细解释之前发生的事情以及之后发生的事情。我猜测,用户被添加,更改或发送新生成的密码。

您应该查看CakeMail:http://book.cakephp.org/2.0/en/core-utility-libraries/email.html

此示例可能有所帮助:

<?php
    //UsersController.php    

        if ($this->request->is('post')){
            $pwBeforeHash = $this->request->data['User']['password'];
            // Uncomment the line below if its a new inserted row in table
            //$this->User->create();
            if($this->User->save($this->request->data)){

                $Email = new CakeEmail();
                $Email->from(array('me@example.com' => 'My Site'));
                $Email->to('you@example.com');
                $Email->subject('New password');
                $Email->send('Hi. This is your new password: ' . $pwBeforeHash);

                $this->Session->setFlash(__('Password is sent to your email'));
            } else {
                $this->Session->setFlash(__('Due to an unknown error, the new password could not be sent. Try again.'));
            }
        }
        ?>

如果你真的需要从会话中获得价值,你应该阅读:http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html

密码不应保存在会话中。

如果您需要阅读会话,请使用:$stringToMySession = $this->Session->read('mySession');

答案 1 :(得分:0)

解决.. 在我的控制器中:

$email = new CakeEmail('smtp');
                $email->config(array(
                        // 'port' => 465,
                        'from' => array($this->Session->read('UserLogged')['User']['email'] => $this->Session->read('UserLogged')['User']['name']),
                        'username' => $this->Session->read('UserLogged')['User']['email'],
                        'password' => $this->Session->read('UserLogged')['User']['password']
                ));
                $email->to($this->request->data['BlogPosts']['to']);
                $email->subject($this->request->data['BlogPosts']['subject']);
                $email->send($this->request->data['BlogPosts']['message']);