Session from the Config

Asked

Viewed 35 times

1

I have the following case: I want to send an email from cakephp, but since there is more than one user in the database (each containing a different email), I need to send each user’s email and password to the /app/Config/email.php file, that is, just pick up from the session. There is already a method that captures the user’s data, being $this->Session->read('Usuariologado'), thus being able to pass the rest of the parameters: $this->Session->read('Usuariologado')['User']['email'] and $this->Session->read('Usuariologado')['User']['password']but when trying to pass these session values to the email.php file, it gives an error saying that it is necessary to use this within a function.

email php.:

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

1 answer

1


In fact, you cannot set the properties dynamically straight in the configuration file Email. You need to make it a Controller, for example.

By the method config(), you can both pass a string representing one of the settings defined in these files, or a array with the configuration properties of SMTP.

For that, do something like this:

$sess = $this->Session->read('UsuarioLogado');

$Email = new CakeEmail();
$Email->config(array(
    'host' => 'ssl://smtp.gmail.com',
    'port' => 465,
    'username' => $sess['User']['email'],
    'password' => $sess['User']['senha'],
));
  • Just missed the 'from' field, the rest is all right.. I found it strange not to have something similar in the manual (http://book.cakephp.org/2.0/en/core-utility-libraries/email.html).. Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.