简体   繁体   中英

Not able to send a view/template through email in codeigniter

i have a codeigniter project through which i wish to send emails to the user.I wish to send the templates having html code effect, however inside the email i am getting html code, instead of view. Can anyone please tell how to do so

Code that i am using is

View

<?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(); ?>

Controller

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

    }

Model

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(); 

            }

    }

Can anyone tell how to send template in email through codeigniter

forgot_password_email view

<!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>

There is a third optional parameter lets you change the behavior of the function so that it returns data as a string rather than sending it to your browser. This can be useful if you want to process the data in some way. If you set the parameter to true (boolean) it will return data. The default behavior is false, which sends it to your browser. Remember to assign it to a variable if you want the data returned:

Change

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

To

$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()

    }

For more https://codeigniter.com/user_guide/general/views.html#returning-views-as-data

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

这将视图转换为字符串。

Add these lines right after loading library with 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM