如何在codeigniter中发送html格式的电子邮件我有这个代码,它正在发送电子邮件,但它没有格式化它应该看起来!你可以看到显示收到的电子邮件的图片
function email_sender()
{
// $this->load->helper('form');
// $this->load->helper('url');
$config = Array(
'protocol' => 'sendmail',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => '465',
'smtp_timeout' => '7',
'smtp_user' => '****@****.com',
'smtp_pass' => '*******',
'charset' =>'utf-8',
'newline' => "\r\n",
'MIME-Version' => '1.0',
'mailtype' => 'html',
'header' => 'MIME-Version: 1.0',
'header' => 'Content-type:text/html;charset=UTF-8'
);//initializing mail headers and information
if ($this->input->post('submit')==true)
{
$data['name']=$this->input->post('contact-name');
$data['email']=$this->input->post('contact-email');
$data['phone']=$this->input->post('contact-tel');
$data['country']=$this->input->post('contact-country');
$data['message']=$this->input->post('exampleTextarea');
}//input from form
$html_formattedEmail = $this->load->view('includes/email_template', $data, TRUE);//variable that loads view and save it as string
$this->load->library('email', $config);
// $this->email-> set_newline("\r\n");
$this->email->from('abc@abc.com');
$this->email->to($data['email']);
$this->email->subject('Contact form submitted');
$this->email->message($html_formattedEmail);
if($this->email->send()){
$data['main_content'] = 'thankyou_page';
$data['meta_title'] = 'abc';
$data['meta_description'] = 'abc2';
$data['meta_author'] = 'abc3';
$this->load->view('includes/template', $data);
}
else{
show_error($this->email->print_debugger());
}
}
答案 0 :(得分:3)
试试这个
// load email library
$this->load->library('email');
//准备电子邮件
$this->email
->from('ex@example.com', 'Example Test.')
->to('toex@example.com')
->subject('Hello Sample Test.')
->message('Hello, We are <strong>Htl content</strong>')
->set_mailtype('html');
// send email
$this->email->send();
您还可以使用视图文件
$this->email
->from('ex@example.com', 'Example Test.')
->to('toex@example.com')
->subject('Hello Sample Test.')
->message($this->load->view('email_template-name', $dynamic-data, true))
->set_mailtype('html');
答案 1 :(得分:2)
您可以尝试以下代码行,将邮件类型设置为HTML
:
$this->email->set_mailtype("html");
答案 2 :(得分:1)
$config = Array(
'mailtype' => 'html'
);
将此添加到您的邮件配置并重新检查。
答案 3 :(得分:0)
在 CodeIgniter 4.1.1 中
这应该可以工作,但完全被忽略了:
$email_config = [
'mailType' => 'html'
];
$email->initialize($config);
所以在你加载了 Altmessage 之后,你可以使用这个完全没有记录的函数:
$email->setMailType("html");
然后像往常一样加载您的 html 版本消息 - 如果没有此页面上提供的其他答案,我将无法猜测。