CakeEmail - 设置多个收件人

CakeEmail - Set multiple recipients

我有下一个问题。我一直在尝试将多个收件人放入我设置的变量中,但未触发邮件。我只对一个收件人进行了测试,并且有效。 当我尝试再添加一个时,不会发送电子邮件。这是定义发送简单电子邮件的第一个函数。这位于我的 AppController.php 内 电子邮件发送是使用指定的 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;      
    }
}

此外,我将在通过表单触发 ajax 请求时准备电子邮件并将其发送给收件人的功能附加给您。

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

 }

如果你们中的任何人对此事有任何想法,我们将不胜感激。我有点卡住了,因为我想尝试包括其他收件人,但不幸的是我认为它必须与 CakeEmail class 有某种兼容性。 干杯

如果您需要向多个收件人发送电子邮件,您需要将他们指定为数组。

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

来自Docs

'to': Email or array of destination.