How to send multiple letters at a time in cakephp

I need to send a few letters at a time, can anyone have an example? or any idea? I need to send mail to all my site users at a time (mail content is the same for everyone)

I am currently using the following code in a for loop

$this->Email->from = '< no-reply@noreply.com >'; $this->Email->to = $email; $this->Email->subject = $subject ; $this->Email->sendAs = 'html'; 
+4
source share
3 answers

In Cakephp 2.0, I used the following code:

 $result = $email->template($template, 'default') ->emailFormat('html') ->to(array(' first@gmail.com ', ' second@gmail.com ', ' third@gmail.com '))) ->from($from_email) ->subject($subject) ->viewVars($data); 
+3
source

I think you have 2 possibilities:

Eogeasp

Suppose you have a mail_users function in your UsersController

 function mail_users($subject = 'Sample subject') { $users = $this->User->find('all', array('fields' => array('email')); foreach ($users as $user) { $this->Email->reset(); $this->Email->from = '< no-reply@noreply.com >'; $this->Email->to = $user['email']; $this->Email->subject = $subject ; $this->Email->sendAs = 'html'; $this->Email->send('Your message body'); } } 

In this function, $this->Email->reset() is important.

using bcc

 function mail_users($subject = 'Sample subject') { $users = $this->User->find('all', array('fields' => array('email')); $bcc = ''; foreach ($users as $user) { $bcc .= $user['email'].','; } $this->Email->from = '< no-reply@noreply.com >'; $this->Email->bcc = $bcc; $this->Email->subject = $subject; $this->Email->sendAs = 'html'; $this->Email->send('Your message body'); } 

Now you can simply call this method with a link to /users/mail_users/subject

For more information, read the Email Component guide.

+12
source

Try the following:

 $tests = array(); foreach($users as $user) { $tests[] = $user['User']['email']; } $mail = new CakeEmail(); $mail->to($tests) ->from('< no-reply@noreply.com >') ->subject('ALERT') ->emailFormat('html') ->send('Your message here'); 
+1
source

All Articles