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

AnN*_*LaI 4 email cakephp bulk cakephp-1.3 cakephp-1.2

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

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

        $this->Email->from     = '<no-reply@noreply.com>';
        $this->Email->to       =  $email;
        $this->Email->subject  =   $subject ;
        $this->Email->sendAs   = 'html'; 
Run Code Online (Sandbox Code Playgroud)

Tim*_*Tim 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');
    }
}
Run Code Online (Sandbox Code Playgroud)

在这个功能中,这$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');
}
Run Code Online (Sandbox Code Playgroud)

现在,您可以通过链接调用此方法 /users/mail_users/subject

有关详细信息,请务必阅读电子邮件组件上的手册.


AnN*_*LaI 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);
Run Code Online (Sandbox Code Playgroud)

  • 带有一组电子邮件地址的“-&gt;to()”可以工作,但应该注意的是,电子邮件会将它们作为“收件人”字段中的地址列表发送,而不是像人们期望的那样作为单独的电子邮件发送。如果您不想向网站上的每个用户发送其电子邮件地址,请考虑使用“-&gt;bcc()”。 (2认同)