How to send a multiple emails at a time in cakephp
Asked Answered
Q

3

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     = '<[email protected]>';
        $this->Email->to       =  $email;
        $this->Email->subject  =   $subject ;
        $this->Email->sendAs   = 'html'; 
Quench answered 2/6, 2011 at 8:6 Comment(0)
Q
4

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);
Quench answered 8/6, 2012 at 12:35 Comment(3)
'->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.Denney
Its static way not dynamic to emails.Theoretician
@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 haQuench
C
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     = '<[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.

Christenechristening answered 2/6, 2011 at 11:23 Comment(0)
Q
4

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);
Quench answered 8/6, 2012 at 12:35 Comment(3)
'->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.Denney
Its static way not dynamic to emails.Theoretician
@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 haQuench
T
0

Try this:

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

$mail = new CakeEmail();
$mail->to($tests) 
    ->from('<[email protected]>')
    ->subject('ALERT')
    ->emailFormat('html')
    ->send('Your message here');
Theoretician answered 6/6, 2014 at 9:39 Comment(3)
Its not a standard way.. using foreachQuench
Can you explain standard way?Theoretician
Don't use foreach.. If you have 100000 users then its not the right format. Take the users email id and pass in To fieldQuench

© 2022 - 2024 — McMap. All rights reserved.