我想发送包含多个数据库值的电子邮件

时间:2012-04-10 10:23:53

标签: codeigniter

   if($records->result()>0) 
    {

        foreach ($records->result() as $user)
        {

            $username= ('first name='.$user->u_first_name.'<br/>'.'Last name='.$user->u_last_name.'<br/>'.'Email='.$user->u_email.'<br/>'.'Property Id='.$user->propertyid);
            $username.="<br/>";
            $username.="-------------------------";
            $username.="<br/>";



            $email_template =  file_get_contents($this->config->item('base_url').'assets/email/email.html');
            $email_template = str_replace("[[EMAIL_HEADING]]", $mail_content->subject, $email_template);
            $email_template = str_replace("[[EMAIL_CONTENT]]", $username, $email_template);
            $email_template = str_replace("[[SITEROOT]]", $this->config->item('base_url'), $email_template);
            $email_template = str_replace("[[LOGO]]",$this->config->item('base_url')."assets", $email_template);
            $this->email->message(html_entity_decode($email_template));      
            $this->email->send();
            print_r($email_template);

这是我的代码

1 个答案:

答案 0 :(得分:2)

/ * UPDATE * /

您可以像普通模板一样使用视图(传入值),将第三个参数设置为TRUE以返回html。

要发送一封包含所有数据库记录的电子邮件,只需将整个结果对象传递到视图中,使用标准foreach循环将视图中的过程传递给视图。

E.g

if($records->result()>0) {
    $email_template = $this->load->view('email_template', array('heading' => 'My Email Report', 'records' => $records->result(), TRUE);
    $this->email->message($email_template);      
    $this->email->send();
    print_r($email_template);
}

然后视图(/ view / email_template)就像;

<h1><?php echo $heading; ?>
<p> Records;</p>
<table>
<?php
foreach ($records as $r) {
?>
<tr>
    <td><?php echo $r->u_first_name; ?></td>
    <td><?php echo $r->u_last_name; ?></td>
    <td><?php echo $r->u_email; ?></td>
    <td><?php echo $r->propertyid; ?></td>
</tr>
<?php
}
?>
</table>