我想使用 codeigniter
库向我的时事通讯成员发送电子邮件,并且我想在电子邮件内容中提及每个成员的电子邮件地址。为此,我使用 foreach 循环
来一封一封地发送电子邮件。 问题是代码只向我的时事通讯的一个成员(第一个成员)发送电子邮件。我检查了我的代码,它从数据库中获取成员并打印出所有成员。
这是我的模型:
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
时,它会一个接一个地打印数据库中的所有电子邮件。但它不会向所有人发送电子邮件。我做错了什么?!
最佳答案
使用
$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,所有附件也将被清除。然后告诉我们
关于codeigniter - 使用 codeigniter 向所有时事通讯成员发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19740023/