5

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     = '<no-reply@noreply.com>';
        $this->Email->to       =  $email;
        $this->Email->subject  =   $subject ;
        $this->Email->sendAs   = 'html'; 
Devjosh
  • 6,450
  • 4
  • 39
  • 61
AnNaMaLaI
  • 4,064
  • 11
  • 53
  • 93

3 Answers3

12

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     = '<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 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     = '<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 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.

Tim
  • 5,893
  • 3
  • 35
  • 64
4

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);
António Almeida
  • 9,620
  • 8
  • 59
  • 66
AnNaMaLaI
  • 4,064
  • 11
  • 53
  • 93
  • 2
    '->to()' with an array of email addresses works, but it should be noted that the email will send them as a list of addresses in the 'to' field - not as individual emails as one might be expecting. Consider using '->bcc()' just in case you don't want to send every user on your site the email address of every user on your site. – Strixy Aug 20 '13 at 20:55
  • Its static way not dynamic to emails. – Indrajeet Singh Jun 24 '14 at 09:44
  • @IndrajeetSingh I given example.. incase of that static array you can pass your dynamic array.. These are very basic. Exactly what you want ? Because I fetched thousands of users from DB directly using Cakephp LIST query and passing to that TO Parameter.. The way you are doing is foreach so thats not the proper way.. I told based on my work exp.. But u down voted my answer .. ha ha ha – AnNaMaLaI Jun 25 '14 at 05:05
0

Try this:

$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');
António Almeida
  • 9,620
  • 8
  • 59
  • 66
Indrajeet Singh
  • 2,958
  • 25
  • 25