【问题标题】:Error: Call to a member function viewVars() on array on CakePHP错误:在 CakePHP 中调用数组上的成员函数视图 Cars()
【发布时间】:2015-04-21 03:13:00
【问题描述】:

我正在尝试使用 cakephp 发送电子邮件。 我的设置代码如下。如果我将配置切换为 gmail 或 fast,或者将 viewVars 添加到其他电子邮件设置,这并不重要 - 我收到此错误:

Call to a member function viewVars() on array   

如果我从 new CakeEmail() 块中删除 viewVars 行 - 它给了我

 Call to a member function send() on array  

这让我觉得我的设置有问题?

我在控制器中设置了这样的电子邮件:

$email = new CakeEmail();
            $email->template('remember_password', 'default')
                    ->config('default')
                    ->emailFormat('html')
                    ->subject(__('Remember password - ' . Configure::read('Application.name')))
                    ->to($user['User']['email'])
                    ->from(Configure::read('Application.from_email'))
                    ->viewVars(array('hash' => $hash))
                    ->send();

设置如下:

class EmailConfig {

  public $default = array(
    'transport' => 'Mail',
    'from' => 'myemail@email.com',
    //'charset' => 'utf-8',
    //'headerCharset' => 'utf-8',
    );

  public $smtp = array(
    'transport' => 'Smtp',
    'from' => array('myemail@email.com' => 'My Site'),
    'host' => 'localhost',
    'port' => 25,
    'timeout' => 30,
    'username' => 'user',
    'password' => 'secret',
    'client' => null,
    'log' => false,
    //'charset' => 'utf-8',
    //'headerCharset' => 'utf-8',
    );

  public $gmail = array(
    'host' => 'ssl://smtp.gmail.com',
    'port' => 465,
    'username' => 'myemail@email.com',
    'password' => 'password',
    'transport' => 'Smtp',
    );

  public $fast = array(
    'from' => 'myemail@email.com',
    'sender' => null,
    'to' => null,
    'cc' => null,
    'bcc' => null,
    'replyTo' => null,
    'readReceipt' => null,
    'returnPath' => null,
    'messageId' => true,
    'subject' => null,
    'message' => null,
    'headers' => null,
    'viewRender' => null,
    'template' => false,
    'layout' => false,
    'viewVars' => null,
    'attachments' => null,
    'emailFormat' => null,
    'transport' => 'Smtp',
    'host' => 'localhost',
    'port' => 25,
    'timeout' => 30,
    'username' => 'user',
    'password' => 'secret',
    'client' => null,
    'log' => true,
    //'charset' => 'utf-8',
    //'headerCharset' => 'utf-8',
    );

【问题讨论】:

  • 我有类似的问题@itamar。尝试像这样在您的 viewVars 中包含compact()function 并再次朗读您的颂歌:->viewVars(compact('hash'));。 CakePHP 会理解 $hash 变量属于 'hash'quoted 在这个函数内通过你的视图
  • @bcesars 这是一个很好的猜测,但 ndms 回答解决了它。

标签: php email cakephp


【解决方案1】:

Application.from_email 不存在或者是null,导致CakeEmail::from() 接收null 作为参数,导致它充当getter,即它将返回当前的from 值,这是一个数组,因此错误。

【讨论】:

  • 谢谢 - 这就是答案!
  • 在尝试了一切并且一开始忽略了这个回复之后,我仔细阅读了这个回复,解决了我的问题,谢谢@ndm
猜你喜欢
  • 1970-01-01
  • 2014-08-23
  • 2012-02-06
  • 1970-01-01
  • 1970-01-01
  • 2018-05-19
  • 1970-01-01
  • 1970-01-01
  • 2022-01-05
相关资源
最近更新 更多