CakePHP: sending email to multiple addresses

I want to send CakeEmail an email to several addresses (the email address of the people who have registered on my site).

Here is my code that I use:

public function send($d){
    $this->set($d);
    if($this->validates()){
        App::uses('CakeEmail','Network/Email');

        $users = $this->User->find('all');

        $this->set($tests);
        foreach($users as $user)
        {
            $tests .= '"'.$user['User']['email'].'",';
        }

        $mail = new CakeEmail();
        $mail
            ->to(array($tests)) 
            ->from(array('test2@test.fr' => 'Hello'))
            ->subject('ALERTE')
            ->emailFormat('html')
            ->template('ouverture')->viewVars($d);
            return $mail->send();

        }

    else{
        return false;
    }

    }

And here is my mistake:

Invalid email : ""test@test.com","test@test.fr","
+4
source share
2 answers

Try

$tests = array();
foreach($users as $user)
{
    $tests[] = $user['User']['email'];
}

$mail = new CakeEmail();
$mail->to($tests) 
     ->from(array('test2@test.fr' => 'Hello'))
     ->subject('ALERTE')
     ->emailFormat('html')
     ->send('Your message here');
+8
source

Try

$mail = new CakeEmail();

foreach($users as $user) {
    $mail->addTo($user['User']['email']);
}

$mail->from(array('test2@test.fr' => 'Hello'))
     ->subject('ALERTE')
     ->emailFormat('html')
     ->template('ouverture')->viewVars($d);
return $mail->send();
+3
source

Source: https://habr.com/ru/post/1538619/


All Articles