如何在cakephp中一次发送多封电子邮件

时间:2011-06-02 08:06:58

标签: email cakephp cakephp-1.3 cakephp-1.2 bulk

我需要一次发送多封电子邮件,任何人都有例子吗?或任何想法? 我需要一次向所有网站用户发送邮件(邮件内容对所有人来说都是一样的)

目前我在for循环中使用以下代码

        $this->Email->from     = '<no-reply@noreply.com>';
        $this->Email->to       =  $email;
        $this->Email->subject  =   $subject ;
        $this->Email->sendAs   = 'html'; 

3 个答案:

答案 0 :(得分:12)

我认为你有两种可能性:

<强>的foreach

假设您的mail_users

中有一个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');
    }
}

在此功能中,$this->Email->reset()很重要。

使用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');
}

现在,您只需使用指向/users/mail_users/subject

的链接调用此方法即可

有关详细信息,请务必阅读Email Component上的手册。

答案 1 :(得分:4)

在Cakephp 2.0中,我使用了以下代码:

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

答案 2 :(得分:1)

试试这个:

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