我需要一次发送多封电子邮件,有人可以举个例子吗?或任何想法?
我需要一次向所有站点用户发送邮件(邮件内容对所有人都相同)
目前我在 for 循环中使用以下代码
$this->Email->from = '<no-reply@noreply.com>';
$this->Email->to = $email;
$this->Email->subject = $subject ;
$this->Email->sendAs = 'html';
最佳答案
我认为你有两种可能性:
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()
很重要。使用密件抄送
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 上的手册。 .
关于email - 如何在cakephp中一次发送多封电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6211992/