使用动态smtp选项cakephp发送smtp电子邮件

时间:2014-10-23 08:19:53

标签: email cakephp cakephp-2.0

我可以发送电子邮件配置来自email.php文件的smtp选项,它工作正常。但我想发送电子邮件,其中smtp选项,如主机,端口,用户名和密码来自数据库。

我尝试使用它,但不起作用,只是给出错误No connection could be made because the target machine actively refused it。 smtp选项是正确的。

App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail('smtp');

$Email->smtpOptions = array(
    'host' => $smtpAccount['host'],
    'port' => $smtpAccount['port'],
    'username' => $smtpAccount['username'],
    'password' => $smtpAccount['password'],
);

$Email->to($email);
$Email->template('sending')->emailFormat('both');
$Email->subject($subject);
$Email->viewVars (
    array(
        'content' => $content
    )
);

return $Email->send();

尝试将主机设置为$Email->host($smtpAccount['host']); 给出错误Call to undefined method CakeEmail::host()

由于

1 个答案:

答案 0 :(得分:1)

应该使用

而不是$Email->smtpOptions

$Email->config(array(
    'host' => $smtpAccount['host'],
    'port' => $smtpAccount['port'],
    'username' => $smtpAccount['username'],
    'password' => $smtpAccount['password'],
));