我想使用codeigniter
库向我的简报会员发送电子邮件,我想在电子邮件内容中提及每个成员的电子邮件地址。为此,我使用foreach loop
逐个发送电子邮件。 问题是代码只向我的简报的一个成员(第一个成员)发送电子邮件。我已经检查了我的代码,它从数据库中获取成员并打印出所有成员。
这是我的模特:
function send_news()
{
$subscribers = $this->get_subscriber_data();
foreach($subscribers as $subscriber)
{
$this->load->helper('typography');
//Format email content using an HTML file
$data['news_Title'] = $this->input->post('news_Title');
$HTML_Message = $this->load->view('admin/includes/newsletter_html_format', $data, true);
$config['mailtype'] = 'html';
$this->email->initialize($config);
$this->email->from('newsletter@site.com', 'newsletter of site.com');
$this->email->to($subscriber->subscriber_Email);
$this->email->subject('newsletter of site.com');
$this->email->message($HTML_Message);
return $this->email->send();
}
}
这就是我获得订阅者名单的方式:
function get_subscriber_data($options = array())
{
$query = $this->db->get('mg_newsletter');
if(isset($options['subscriber_Id']))
return $query->row(0);
return $query->result();
}
当我尝试回显$subscriber->subscriber_Email
时,它会一个接一个地打印数据库中的所有电子邮件。但它不向所有人发送电子邮件。我做错了什么?!
答案 0 :(得分:3)
使用
$this->email->clear()
正如codeignitor所说: 将所有电子邮件变量初始化为空状态。如果您在循环中运行电子邮件发送功能,允许在循环之间重置数据,则可以使用此功能。
foreach ($list as $name => $address)
{
$this->email->clear();
$this->email->to($address);
$this->email->from('your@example.com');
$this->email->subject('Here is your info '.$name);
$this->email->message('Hi '.$name.' Here is the info you requested.');
$this->email->send();
}
如果将参数设置为TRUE,则也会清除任何附件。然后告诉我们
答案 1 :(得分:1)
你在循环中“返回” - 退出函数。
发送
答案 2 :(得分:0)
您可以将加载助手文件和电子邮件配置保留在循环之外,然后从for循环中删除返回,它将逐个发送给所有订阅者,然后在循环结束后,您可以执行return语句。