无法通过codeigniter中的电子邮件发送视图/模板

时间:2017-01-03 06:19:40

标签: php codeigniter email model-view-controller

我有一个codeigniter项目,我希望通过该项目发送电子邮件给用户。我希望发送具有HTML代码效果的模板,但是在电子邮件中我得到的是html代码,而不是查看。任何人都可以告诉你该怎么做

我正在使用的代码是

查看

<?php echo form_open('home/forgot_pass'); ?>
    <div class="form-group">
        <?php
            $data = array(
                'type'=>'text', 
                'class' => "form-control",
                'name'=>'email',
                'autocomplete'=>'off', 
                'placeholder'=>'Email' 
                );
            ?>
        <?php echo form_input($data); ?>
    </div>

    <div class="form-group">
        <?php
            $data = array(
                'type'=>'submit',
                'class'=>'btn btn-success btn-block',
                'name'=>'submit',
                'content'=>'Login'
            );

            echo form_button($data); 
        ?>
    </div>
<?php echo form_close(); ?>

控制器

public function forgot_pass()
    {
        $this->user_model->forgot_pass();

    }

模型

public function forgot_pass()
    {
        $email = $this->input->post('email');
        $this->db->where('email',$email);
        $query = $this->db->get('users');
        $result = $query->row();

        if($result)
            {
                $password = $result->password;

                $from_email = "xyz@gmail.com"; 

                $this->load->library('email'); 

                $this->email->from($from_email, 'ABC');
                $data = array(
                'password'=> $password
                );

                $this->email->to($email); 
                $this->email->subject('Forgot Password');  

                $body = $this->load->view('users/forgot_password_email',$data,TRUE);
                $this->email->message($body);   
                $this->email->send(); 

            }

    }

任何人都可以通过codeigniter告诉您如何通过电子邮件发送模板

forgot_password_email查看

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

</head>
<body>

    <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation" style="padding: 8px;">

        <span style="color:white;text-align:center;margin-left: 45%;font-size: 20px;font-weight: 600;">LITEHIRES</span>

    </nav>

    <div class="container" style="margin-left: 25%; margin-top: 4%;">
        <p style="font-weight: 600; font-size: 18px;">Hi Samya</p>

            <p> You have recently requested for a password on your litehires account. Please use the following password for your account</p>
            <p style=" margin-left: 25%; color: #3498db; font-size: 16px; "><?php echo $password; ?></p>

            <p>Thanks</p>
            <p style=" margin-top: -10px; ">Litehires Team</p>

    </div>

</body>
</html>

3 个答案:

答案 0 :(得分:2)

第三个可选参数允许您更改函数的行为,以便将数据作为字符串返回,而不是将其发送到浏览器。如果要以某种方式处理数据,这可能很有用。如果将参数设置为true(布尔值),它将返回数据。默认行为为false,将其发送到您的浏览器。如果要返回数据,请记住将其分配给变量:

更改

$body = $this->load->view('users/forgot_password_email');

$body = $this->load->view('users/forgot_password_email','',true);

MODEL

public function forgot_pass()
    {
        $from_email = "xyz@gmail.com"; 
        $to_email = trim($this->input->post('email')); //use trim to removes spaces from both sides 
        $config['mailtype'] = 'html';
        $this->load->library('email',$config); 

        $this->email->from($from_email, 'Your Name'); 
        $this->email->to($to_email);
        $this->email->subject('Email Test'); 
        $data['name'] = 'name here'; //post from from
        $data['password'] = 'password'; //new generated password here
        $body = $this->load->view('users/forgot_password_email',$data,true);
        $this->email->message($body);  
        $this->email->send()

    }

了解更多https://codeigniter.com/user_guide/general/views.html#returning-views-as-data

答案 1 :(得分:0)

$body = $this->load->view('users/forgot_password_email' ,true);

将视图转换为字符串。

答案 2 :(得分:0)

在使用config加载库之后立即添加这些行。

$this->email->set_header('MIME-Version', '1.0; charset=utf-8'); //must add this line
$this->email->set_header('Content-type', 'text/html'); //must add this line