I need to send multiple emails at a time, can any one have example? or any idea ? I need to send mail to all my site users at a time (Mail content is same for all)
Currently i using following code in a for loop
$this->Email->from = '<[email protected]>';
$this->Email->to = $email;
$this->Email->subject = $subject ;
$this->Email->sendAs = 'html';
use Cake\Mailer\Email; After you've loaded Email , you can send an email with the following: $email = new Email('default'); $email->from(['[email protected]' => 'My Site']) ->to('[email protected]') ->subject('About') ->send('My message');
Sending messages quickly Example: CakeEmail::deliver('[email protected]', 'Subject', 'Message', array('from' => '[email protected]')); This method will send an email to [email protected], from [email protected] with subject Subject and content Message. The return of deliver() is a CakeEmail instance with all configurations set.
I think you have 2 possibilities:
foreach
Let's assume you have a function mail_users
within 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 = '<[email protected]>';
$this->Email->to = $user['email'];
$this->Email->subject = $subject ;
$this->Email->sendAs = 'html';
$this->Email->send('Your message body');
}
}
In this function the $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 = '<[email protected]>';
$this->Email->bcc = $bcc;
$this->Email->subject = $subject;
$this->Email->sendAs = 'html';
$this->Email->send('Your message body');
}
Now you can just call this method with a link to /users/mail_users/subject
For more information be sure to read the manual on the Email Component.
In Cakephp 2.0 I used the following code:
$result = $email->template($template, 'default')
->emailFormat('html')
->to(array('[email protected]', '[email protected]', '[email protected]')))
->from($from_email)
->subject($subject)
->viewVars($data);
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