简体   繁体   中英

Invalid email: “Array” CakePhp CakeEmail

Sorry before, i have a function add answer for answering question in my article, so when user add answer for a question, system will sending an email to user who wrote this question. This is my function in controller :

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

class Question extends 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;
}

}

But when run this code, there is error "Invalid Email 'Array'", i have been check my query, when i run my query in navicat is fine, any help will be greatly appreciated thanks

Concording with 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**

So your $user array must like : ["email1@mail.com"=>"Name User", "email2@mail.com"=>"Name User2"] or a string : "email@mail.com" .

Try this ! and tell me more about.

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

If isnt work , please tell us more about your bug

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM