Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a multiple emails at a time in cakephp

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'; 
like image 469
AnNaMaLaI Avatar asked Jun 02 '11 08:06

AnNaMaLaI


People also ask

How to send email in cakephp?

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

How can I send email in cakephp 2?

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.


2 Answers

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.

like image 114
Tim Avatar answered Oct 08 '22 09:10

Tim


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);
like image 39
AnNaMaLaI Avatar answered Oct 08 '22 10:10

AnNaMaLaI