在CodeIgniter邮件中格式化HTML

3

我需要向用户发送一封HTML格式的电子邮件。我已经阅读了其他帖子,但没有帮助。

这是我的控制器代码:

public function reply_post(){

    $to = $this->input->post('contact_email');
    $subject = $this->input->post('contact_subject');
    $header_message = "<html><head><title>".$subject."</title></head><body>";
    $footer_message = "</body></html>";
    $input_msg = $this->input->post('contact_reply');
    $msg = $header_message.$input_msg.$footer_message;
    $from = "admin@XXXXXXX.com";

    $config = Array(
      'protocol' => 'smtp',
      'smtp_host' => 'mail.XXXXXX.com',
      'smtp_port' => XXX,
      'smtp_user' => 'admin@XXXXX.com', // change it to yours
      'smtp_pass' => 'XXXXX', // change it to yours
      'mailtype' => 'html',
      'charset' => 'iso-8859-1',
      'wordwrap' => TRUE
    );

    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
    $this->email->from($from, "Admin"); // change it to yours
    $this->email->to($to);// change it to yours
    $this->email->subject($subject);
    $this->email->message($msg);

    if($this->email->send()){
      $msg = 'Email sent.';
    } else {
        log_message('error','Email did not send');
        $msg = "Email Did not send";
    }
    $this->finish($msg);
}

我从表单中获取的消息是通过Summernote所见即所得编辑器传递的,因此它已经格式化为HTML。然而,当我收到电子邮件时,它不会应用HTML标签,所有标签都可见。好像邮件类型被设置为“文本”,即使在配置中将其设置为HTML。


1
尝试添加 $this->email->set_mailtype("html"); - Linus
这不是已经在配置数组中添加了“mailtype => html”吗? - cyberrspiritt
它确实起作用了@AnmolRaghuvanshi。不介意的话,你能解释一下这是为什么吗?因为它已经存在于配置文件中了。 - cyberrspiritt
我之前使用的是summernote,所以HTML输出没有被渲染。我已经在Bootstrap所提供的所见即所得编辑器中尝试了相同的代码,那里运行良好。 - cyberrspiritt
请将解释作为答案发布到此问题中 - cyberrspiritt
Codeigniter完整的HTML邮件配置:https://dev59.com/-m3Xa4cB1Zd3GeqPcDaG#38740292 - Pran
1个回答

3

如果你没有初始化配置选项,那么设置配置选项是无效的。在 $config 数组后面添加以下内容:

$this->email->initialize($config);

所以你没有初始化它,因此它无法正常工作。进一步添加:

 adding $this->email->set_mailtype("html");

也许可以解决您的问题。

加载库,然后初始化


为什么我使用Bootstrap所提供的所见即所得编辑器发送数据时可以正常工作,但是在Summernote中却无法正常工作? - cyberrspiritt
配置已经传递给库加载函数。它初始化了一切,除了邮件类型? - cyberrspiritt
@cyberrspiritt 顺便说一下,我不知道 Bootstrap WYSIWYG 和 Summernote。 - Linus
通过将偏好值数组传递给电子邮件初始化函数来设置首选项。请参阅文档“设置电子邮件首选项”https://ellislab.com/codeigniter/user-guide/libraries/email.html - Linus
好的...谢谢你的帮助。 - cyberrspiritt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接