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

发布于 2024-11-12 06:10:28 字数 463 浏览 3 评论 0原文

我需要一次发送多封电子邮件,有人可以举个例子吗?或者有什么想法吗? 我需要一次向所有网站用户发送邮件(所有邮件内容都相同)

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

        $this->Email->from     = '<[email protected]>';
        $this->Email->to       =  $email;
        $this->Email->subject  =   $subject ;
        $this->Email->sendAs   = 'html'; 

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疯到世界奔溃 2024-11-19 06:10:28

我认为你有两种可能性:

foreach

让我们假设你的 UsersController 中有一个函数 mail_users

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

在这个函数中 $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     = '<[email protected]>';
    $this->Email->bcc      = $bcc;
    $this->Email->subject  = $subject;
    $this->Email->sendAs   = 'html';
    $this->Email->send('Your message body');
}

现在,您只需通过 /users/mail_users/subject 的链接调用此方法即可。

有关详细信息,请务必阅读 电子邮件组件

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.

捎一片雪花 2024-11-19 06:10:28

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

$result = $email->template($template, 'default')
    ->emailFormat('html')
    ->to(array('[email protected]', '[email protected]', '[email protected]')))
    ->from($from_email)
    ->subject($subject)
    ->viewVars($data);

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);
猫卆 2024-11-19 06:10:28

试试这个:

$tests = array();
foreach($users as $user) {
    $tests[] = $user['User']['email'];
}

$mail = new CakeEmail();
$mail->to($tests) 
    ->from('<[email protected]>')
    ->subject('ALERT')
    ->emailFormat('html')
    ->send('Your message here');

Try this:

$tests = array();
foreach($users as $user) {
    $tests[] = $user['User']['email'];
}

$mail = new CakeEmail();
$mail->to($tests) 
    ->from('<[email protected]>')
    ->subject('ALERT')
    ->emailFormat('html')
    ->send('Your message here');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文