简体   繁体   中英

CakeEmail - Set multiple recipients

I have the next problem. I have been trying to put more than one recipient in a variable which I have set, but the mail is not triggered. I have tested this with just one recipient and it works. When I try to add one more, the email won't be sent. This is the first function that defines the sending of a simple email. This is located inside my AppController.php The email sending is done with the specified class CakeEmail.

   public function sendSimpleMail($to, $subject, $body, $to_copy = "") {
    $to = trim($to);
    $replay_to = EMAIL_REPLY;

    try {
        App::uses('CakeEmail', 'Network/Email');
        $email = new CakeEmail();
        if($to_copy == ""){
            $email->config('smtp')
            ->to($to)
            ->subject($subject)
            ->replyTo($replay_to)
            ->emailFormat('html');
        }
        else{
            $email->config('smtp')
            ->to($to)
            ->subject($subject)
            ->replyTo($replay_to)
            ->bcc($to_copy)
            ->emailFormat('html');
        }


        $email->send($body);
        return true;

    } catch (Exception $e) {
        $error = $e->getMessage();
        return $error;      
    }
}

Moreover, I am going to attach you the function where I prepare the email and send it to the recipients when the ajax request is trigerred through a form.

   public function wallet_request_ajax($email_callback){
    $this->autoRender = false;
    $d = $this->request->data;

    if(empty($d['date'])){
        return json_encode([
            'id' => 400,
            'txt' => __('Please, insert a date for the department to call you.')
        ]);
    }

    if(empty($d['time'])){
        return json_encode([
            'id' => 400,
            'txt' => __('Please specify an hour for the call.')
        ]);
    }

    if(empty($d['phone'])){
        return json_encode([
           'id' => 400,
            'txt' => __('Please, specify a phone number.')
        ]);
    }

    $existId = $this->Member->find('first', [
        'recursive' => -1,
        'fields' => ['id', 'name', 'surname'],
        'conditions' => [
            'md5(id)' => $d['id']
        ]
    ]);

    if(empty($existId)){
        return json_encode([
            'id' => 400,
            'txt' => __('Unexpected error. Contact with '. TELEPHONE) //TELEPHONE constant 

        ]);
    }
    $member_name = $existId['Member']['name'];
    $member_surname = $existId['Member']['surname'];
    $this->set(compact('member_name', 'member_surname'));
    $final_name = $member_name . " ".$member_surname;**

    $this->loadModel('PhoneCall');
    $this->PhoneCall->create();
    if($this->PhoneCall->save([
        'phone' => $d['phone'],
        'id' => $existId['Member']['id'],
        'date' => date('Y-m-d', strtotime($d['date'])),
        'time' => $d['time']
    ])){
     // We send the email to 'customer care'
    if (PAIS === 'es'){   // if country is Spain 
        $to = "blabla12@hotmail.com, etc@gmail.com ,blabla@gmail.com"; // recipients
    } elseif(PAIS == 'it') {  // if country is Italy
        $to = "etc@gmail.com"; // recipients
    }elseif(PAIS === 'en'){ // if country is UK
        $to = "blabla12@hotmail.com"; // recipients
    }

    $subject = "Activation service";
    $body = "";
    $body .= "--------Notification of activation service";
    $body .= "-------<br>";
    if ($final_name !== "") {
        $body .= "<tr><td>Name: " . $final_name . "</td></tr><br>";
    }
    $body .= "----------------------------<br>";
    $body .= "<tr><td>Date: " . $d['date'] . "<br></td></tr>";
    $body .= "----------------------------<br>";
    $body .= "<tr><td>".__('Time'). ":" . $d['time'] . "<br></td></tr>";
    $body .= "----------------------------<br>";
    $body .= "<tr><td>Phone: " . $d['phone'] . "</td></tr><br>";
    $body .= "----------------------------<br>";
    $body .= "----------------------------<br>";
    $email_callback = $this->sendSimpleMail($to, $subject, $body);

    return json_encode([
        'id' => 200,
        'txt' => __('We will call you the specified time.')
    ]);

 }

If anyone of you out there has any thoughts as regards the matter, it would be really appreciating. I am kind of stuck as I want to try to include other recipients but unfortunately I think it must be some kind of compatibility with the CakeEmail class. Cheers

If you need to send email to multiple recipients, you need to specify them as array.

            $email->config('smtp')
            ->to( array('first@email.com', 'second@email.com'));

From the Docs

'to': Email or array of destination.

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