【发布时间】:2017-08-28 12:15:34
【问题描述】:
我想给一些人发送电子邮件简报,他们的状态在数据库中是活动的,我已经完成了,但我也想在每封电子邮件中发送一个取消订阅链接,我也做了,但我得到了一些东西foreach 循环中的错误,因为每次循环运行时,它都会通过电子邮件向用户发送一个额外的 (i+1) 取消订阅链接。我附上代码。
这是我的视图
<form role="form" method="post" action="<?= base_url()?>email/send_newsletter">
<div class="box-body">
<div class="form-group">
<label>Select Multiple Lists</label>
<select class="form-control select2" name="lists" data-placeholder=" Select List(s)">
<?php foreach ($lists as $list_name){ ?>
<option value="<?php echo $list_name['list_id'];?>"><?php echo $list_name['list_name'];?></option>
<?php }?>
</select>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Subject</label>
<input type="text" name="subject" class="form-control" id="exampleInputEmail1" placeholder="Enter Subject">
</div>
<script src="<?= base_url()?>assets/ckeditor/ckeditor.js"></script>
<div class="form-group">
<label for="exampleInputEmail1">Message</label>
<textarea name="message" class="form-control" id="editor1" cols="10" rows="5"></textarea>
<script>
CKEDITOR.replace( 'editor1' );
</script>
</div>
<div class="form-group">
<label for="exampleInputFile">File input</label>
<input type="file" id="exampleInputFile">
<p class="help-block">Example block-level help text here.</p>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
这是我的控制器方法
public function send_newsletter()
{
$lists = $this->input->post('lists');
$subject = $this->input->post('subject');
$message = $this->input->post('message');
$join_str1 = "subscribers.subscriber_list_id=lists.list_id";
$subscribers =
$this->global_model
->join_2table('subscribers','lists', $join_str1,['subscriber_list_id'=>$lists,'subscriber_status'=>'Active']);
foreach($subscribers as $row) {
$email_lists = $row['subscriber_email'];
$random_key = $row['random_key'];
$message.=
"<a href=\"http://xyz.in/abc/unsubscribe/unsubscribe_me/{$random_key}\">Unsubscribe Here</a>";
$from_email = 'support@xyz.com';
$this->email->from($from_email, 'CRM');
$this->email->to($email_lists);
$this->email->subject($subject);
$this->email->message($message);
$this->email->set_mailtype('html');
$sendmail = $this->email->send();
}
//Send mail
if($sendmail)
{
echo "Email sent";
}
else
{
echo "email failed.";
}
}
【问题讨论】:
-
嗯,您在每次循环迭代中都向 $message 附加了一个新链接 - 那么您还期望发生什么……?
-
您是否尝试过使用 $this->email->clear(TRUE) 在这里解释 codeigniter.com/user_guide/libraries/email.html#CI_Email::clear
-
感谢您的帮助
标签: php arrays codeigniter email foreach