我想将视图传递给字符串,并使用codeigniter作为邮件正文传递给电子邮件。有什么建议?

时间:2018-05-02 09:01:38

标签: codeigniter email-templates

我想在注册用户时发送电子邮件模板。所以我为它创建了视图,以便可以根据需要使用不同的视图。喜欢注册和忘记密码,有两种不同的观点。

public function email($data, $type){

$smtp = $this->smtp_model->smtp_data();

$config['protocol']  = 'smtp';
$config['smtp_host'] = $smtp->host;
$config['smtp_port'] = $smtp->port;
$config['smtp_user'] = $smtp->username; 
$config['smtp_pass'] = $smtp->password;
$config['charset']   = 'utf-8';
$config['mailtype']  = 'html';
$config['newline']   = '\r\n';


$this->email->initialize($config);
$this->email->clear(TRUE);
$this->email->from($smtp->from_email, $smtp->from_name);
$this->email->to($data->email);
$this->email->subject(SITENAME.' - '.$type);

// Here it should return html from the view
$mail_message = $this->email_template($data, 'registration');

echo $mail_message;die;//but it is printing empty

$this->email->message($mail_message);
$this->email->send();
}

public function email_template($data, $type){

$email_body = $this->load->view('email/include/head');
// Below are the condition on basis of it view will be selected 
if($type == 'registration')
    $email_body = $this->load->view('email/user_registration', $data);
if($type == 'forgot_password')
    $email_body = $this->load->view('email/forgot_password', $data);

//Need to pass view to variabel which will be returned to above function
$email_body = $this->load->view('email/include/footer');

return $email_body;

}

2 个答案:

答案 0 :(得分:2)

希望这有助于您:

$this->load->view()的第三个参数设置为TRUE 如果将参数设置为TRUE(布尔值),它将返回数据。默认行为为false,

email_template方法应该是这样的:

public function email_template($data, $type)
{
   $email_body = $this->load->view('email/include/head','',TRUE);
   if($type == 'registration')
   {
      $email_body += $this**strong text**->load->view('email/user_registration', $data,TRUE);
   }
   if($type == 'forgot_password')
   {
        $email_body += $this->load->view('email/forgot_password', $data,TRUE);
   }

   $email_body += $this->load->view('email/include/footer','',TRUE);
   return $email_body;
}

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

答案 1 :(得分:1)

您需要传递true作为第三个参数,因为您将此HTML存储在变量$email_body中。

希望这会有所帮助!!