Codeigniter邮件 - 样式标签/CSS不起作用

4
我正在尝试发送特定div的innerhtml到邮件中(在Codeigniter框架中)。
我使用jquery获取了div的inner html,并使用ajax将数据传输到控制器。
邮件已经发送成功,但我的问题是html类、样式标签(除了我在视图文件html中编写的内联样式),样式表都无法工作。
有没有办法将我的style.css文件添加到电子邮件内容中?即使我在部分开头放置的标签正常工作,我也可以接受。
请帮忙看一下,这是我的代码。
传输html到控制器的Ajax代码:
<script type="text/javascript">
    $(function() {
  $('#sendmail').click(function(e) {

        e.preventDefault();
        $.ajax({
              url:'<?php echo site_url();?>/welcome/send',
              type:'POST',
              data:{'message':$('#body').html(),'subject':'Subject of your e-mail'},
              success:function(data) {
                    alert('You data has been successfully e-mailed');
                    alert('Your server-side script said: ' + data);
              }
        });
  });
});

</script>

控制器中的代码 public function send() {

            $nome = $this->input->post('message');

           $config = array(
            'protocol' => 'smtp',
            'smtp_host' => 'send.one.com',
            'smtp_port' => '2525',
            'smtp_user' => 'akhil.ns@cymose-tech.com',
            'smtp_pass' => 'akhil123',
            'charset' => 'utf-8',
            'wordwrap' => TRUE,
            'mailtype' => 'html'
        );
        $this->load->library('email', $config);

        $this->email->set_newline("\r\n");
        $this->email->from('akhil.ns@cymose-tech.com', 'The Wanderers');
        $this->email->to('shabasy002@gmail.com');

        $this->email->subject('The Wanderers Proforma Invoice ');

        $formatedMessag ='';

    $formatedMessag = '<html><head><link rel="stylesheet" type="text/css" href="http://localhost/study/extra/style.css"></head><body><style type="text/css">p{color:#cccccc;font-weight:bold;}</style>';
        $formatedMessag .= $nome.'</body></html>';

        $this->email->message($formatedMessag);

        if ($this->email->send()) {

            echo $formatedMessag;

            }

}

2
据我所知,您需要坚持使用内联样式。 - Billy Moat
@BillyMoat 没错,还有内部样式 - 请看下面的答案。 - Sergey Telshevsky
1
创建HTML电子邮件时,要在所有客户端中获得相同外观的东西是一个巨大的头痛,信不信由你,它们仍应使用内联样式和表格进行布局,以实现最大兼容性。此外,请记住,大多数电子邮件客户端默认关闭图像。 - Rick Calder
2个回答

3
据我所知,没有哪个电子邮件客户端允许在HTML邮件中加载外部样式表。
最终,这意味着您必须在文档的头部声明样式,例如:
<head>
    <style>
        body { background: black; color: white; }
    </style>
</head>

然而,一些邮件客户端(主要是Outlook)甚至不允许在文档头中定义的某些样式,这意味着需要内联样式,例如:

<body style="background: black; color: white;"></body>

(我的内心深处受到了一些伤害)

即使在HTML邮件中使用内联CSS,也存在许多问题,因为一些客户端(尤其是Outlook)不支持像float之类的简单CSS功能。这意味着您必须使用表格来编写新闻通讯,以确保大多数人都能正常使用。

有关更多信息,请参见本文(或在Google上搜索): http://www.sitepoint.com/code-html-email-newsletters/


0
问题实际上是你只能使用内联内部CSS,这意味着你可以在元素内定义所有的CSS规则,例如:
<td style="padding:10px;"><b style="color:red;">Texty</b></td>

或者在电子邮件文档的<head></head>部分中:

<head>
    <style>
        b {color:red;}
        td { padding:10px; }
    </style>
</head>

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