0

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

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;

}
4

2 回答 2

2

希望这对您有所帮助:

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

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

于 2018-05-02T09:09:55.133 回答
1

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

希望这可以帮助!!

于 2018-05-02T09:09:50.513 回答