簡體   English   中英

無法通過Codeigniter中的電子郵件發送視圖/模板

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

我有一個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>

第三個可選參數使您可以更改函數的行為,以便它以字符串形式返回數據,而不是將其發送到瀏覽器。 如果您想以某種方式處理數據,這可能會很有用。 如果將參數設置為true(布爾值),它將返回數據。 默認行為是false,它將其發送到您的瀏覽器。 如果要返回數據,請記住將其分配給變量:

更改

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

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

模型

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

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

這將視圖轉換為字符串。

在使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM