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

Get Session From /app/Config/email.php

我想使用 CakeEmail 发送电子邮件,但我需要从会话中获取用户电子邮件和来自 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 中读取并且我无法从控制器设置密码...

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

您应该查看 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');

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

$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']);