I want to send an email with CakeEmail to multiple addresses (email address of the people who sign up in my website).
Here is my code 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's my error :
Invalid email : ""test@test.com","test@test.fr","
use Cake\Mailer\Email; After you've loaded Email , you can send an email with the following: $email = new Email('default'); $email->from(['me@example.com' => 'My Site']) ->to('you@example.com') ->subject('About') ->send('My message');
Sending messages quickly Example: CakeEmail::deliver('you@example.com', 'Subject', 'Message', array('from' => 'me@example.com')); This method will send an email to you@example.com, from me@example.com with subject Subject and content Message. The return of deliver() is a CakeEmail instance with all configurations set.
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');
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With