How to send a multiple emails at a time in cakephp

二次信任 提交于 2019-11-28 04:42:29

问题


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

回答1:


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.




回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/6211992/how-to-send-a-multiple-emails-at-a-time-in-cakephp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!