How to send a multiple emails at a time in cakephp

13,771

Solution 1

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);

Solution 2

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.

Share:
13,771
AnNaMaLaI
Author by

AnNaMaLaI

I am a web developer, Worked on various Social community networking websites and E-commerce websites. Knowledge in Zend/Cakephp/Yii/Magento/Joomla/wordpress/Facebook apps/Twitter apps/ Google map Apis/Yelp Apis/ Janrain Apis

Updated on June 09, 2022

Comments

  • AnNaMaLaI
    AnNaMaLaI almost 2 years

    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'; 
    
  • Strixy
    Strixy over 10 years
    '->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.
  • AnNaMaLaI
    AnNaMaLaI almost 10 years
    Its not a standard way.. using foreach
  • Indrajeet Singh
    Indrajeet Singh almost 10 years
    Can you explain standard way?
  • AnNaMaLaI
    AnNaMaLaI almost 10 years
    Don't use foreach.. If you have 100000 users then its not the right format. Take the users email id and pass in To field
  • Indrajeet Singh
    Indrajeet Singh almost 10 years
    Its static way not dynamic to emails.
  • AnNaMaLaI
    AnNaMaLaI almost 10 years
    @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