【问题标题】:Invalid email: "Array" CakePhp CakeEmail无效的电子邮件:“数组”CakePhp CakeEmail
【发布时间】:2015-01-14 00:57:06
【问题描述】:

抱歉,我的文章中有一个添加问题答案的功能,所以当用户添加问题答案时,系统会向撰写此问题的用户发送电子邮件。这是我在控制器中的功能:

public function add($question_id = null) {    
    if ($this->request->is('post')) {
            $this->loadModel('Question');
            $this->Answer->set(array('question_id'=>$question_id));

        $this->Answer->create();

        $user = $this->Question->find('all',array('fields' => 'Useri.email',
                                                  'joins' => array(array('conditions' => array('Useri.id = Question.user_id'),
                                                                                   'table' => 'users',
                                                                                   'alias' => 'Useri',
                                                                                   'type' => 'INNER'))));


            $email = new CakeEmail();
            $email->config('server');
            $email->template('answer');
            $email->viewVars(array(
                    'to'=>$user['User']['email']
            ));
            $email->to($user);
            $email->from(array('gais@wahanaartha.com' => 'IT Sharing Knowledge Team'));
            $email->subject('Sharing Knowledge Notification');
            $result=$email->send();

        if ($this->Answer->save($this->request->data)) {
            $this->Question->updateAll(array('Question.status' => '1'),  
                           array('Question.id' => $question_id));
            $this->Session->setFlash(__('The answer has been saved.'));
            return $this->redirect(array('controller' => 'questions', 'action' => 'view', $question_id));
        } else {
            $this->Session->setFlash(__('The answer could not be saved. Please, try again.'));
            return $this->redirect(array('controller' => 'questions', 'action' => 'view', $question_id));
        }
    }
    $questions = $this->Answer->Question->find('list');
    $users = $this->Answer->User->find('list');
    $this->set(compact('questions', 'users', 'tests'));
}


<?php

App::uses('AppModel', 'Model');

类问题扩展 AppModel {

public $validate = array(
    'topic_id' => array(
        'rule' => 'numeric',
        'required' => true,
        'message' => 'Topic must be choosen !'
    ),
    'user_id' => array(
        'rule' => 'numeric',
        'required' => true,
        'message' => 'User must be choosen !'
    ),
    'question' => array(
        'minLength' => array(
            'rule' => array('minLength', 12),
            'required' => true,
            'message' => 'Question min. 12 characters !'
        ),
        'unique' => array(
            'rule' => array('checkUnique', 'question'),
            'message' => 'Question don\'t be same !'
        )
    ),
    'description' => array(
        'minLength' => array(
            'rule' => array('minLength', 12),
            'required' => true,
            'message' => 'Description min. 12 characters !'
        ),
        'unique' => array(
            'rule' => array('checkUnique', 'description'),
            'message' => 'Description don\'t be same !'
        )
    )
);

public $belongsTo = array(
    'Topic' => array(
        'className' => 'Topic',
        'foreignKey' => 'topic_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

public $hasMany = array(
    'Answer' => array(
        'className' => 'Answer',
        'foreignKey' => 'question_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);

function checkUnique($data, $fieldName) {
    $valid = false;
    if(isset($fieldName) && $this->hasField($fieldName)) {
        $valid = $this->isUnique(array($fieldName => $data));
    }
    return $valid;
}

}

但是当运行此代码时,出现错误“无效电子邮件'数组'”,我一直在检查我的查询,当我在 Navicat 中运行我的查询时很好,任何帮助将不胜感激 谢谢

【问题讨论】:

    标签: cakephp cakephp-2.0 cakeemail


    【解决方案1】:

    与 CakeEmail 一致:

    **to( ) public
    To
    Parameters
    mixed $email optional null
    Null to get, String with email, Array with email as key, name as value or email as value (without name)
    string $name optional null
    Returns
    mixed
    mixed**
    

    所以你的 $user 数组必须像:["email1@mail.com"=>"Name User", "email2@mail.com"=>"Name User2"] 或字符串:“email@mail.com”。

    试试这个!并告诉我更多信息。

    (try to set $email->to(array('mail@mail.com'=>'Name User') );
    

    如果不起作用,请告诉我们更多关于您的错误

    【讨论】:

    • 嗨@MouradK 谢谢之前,我正在尝试您的解决方案并运行 oke,但在我的问题中,我想将该电子邮件发送给在文章中写问题的用户,这意味着只有一个用户写了问题,我尝试使用条件搜索用户电子邮件并设置$email-&gt;to(array($email)),但仍然无效电子邮件“数组”
    • 是用户登录吗?使用 $this->Auth->user() 让您的用户登录,并获取会话信息。或者您可以通过您的模型问题和用户来查看链接(belongsTo 和 cie)
    • 不是我的意思,所以业务流程是当用户写问题时,如果用户有问题,系统会向管理员发送电子邮件通知,然后管理员会创建一个答案对于这个问题并向写这个问题的用户发送电子邮件
    • 所以我创建了查找用户的查询,因为用户有 group_id 2,管理员有 group_id 1
    • 你能把你的问题模型贴出来吗,(收到邮件的是提问者)。
    猜你喜欢
    • 2014-05-06
    • 2013-07-14
    • 2014-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    • 2015-11-07
    相关资源
    最近更新 更多