简体   繁体   English

如何在我从Perl CGI脚本发送的HTML电子邮件中包含CSS文件?

[英]How do I include a CSS file with HTML email that I send from a Perl CGI script?

I am using following code to create and send email through CGI perl. 我使用以下代码通过CGI perl创建和发送电子邮件。 The images get attached properly, but the css files do not. 图像正确附加,但css文件没有。

  my $msg = new MIME::Lite(
            From    => $from,
            To      => $to_list,
            Subject => "My subject",
            Type    => 'text/html',  # 'multipart/mixed'
            Data    => $body
           );
 $msg->attach(Type => 'multipart/mixed',      Path => $DOCS_URL . "/fonts-min.css");
 $msg->attach(Type => 'multipart/mixed',      Path => $DOCS_URL . "/eat.css");
 $msg->attach(Type => 'multipart/mixed',      Path => $DOCS_URL . "/site_styles2.css");
 $msg->attach(Type => 'multipart/mixed',      Path => $DOCS_URL . "/calendar_layout.css");
 $msg->attach(Type => 'multipart/mixed',      Path => $DOCS_URL . "/errnacc.css");
 $msg->attach(Type => 'image/png',    Path => $DOCS_URL . "/separator.gif");
 $msg->attach(Type => 'image/png',    Path => $DOCS_URL . "/trans_pixel.gif");
 $msg->attach(Type => 'image/png',    Path => $DOCS_URL . "/itg_side.gif");

 $msg->send();

The images get attached properly, but the css files are not attached at all. 图像正确附加,但css文件根本没有附加。 Any idea what 'Type' should be used to attach CSS files? 知道什么'Type'应该用于附加CSS文件? Or is there any other problem? 还是有其他问题吗?

我相信text / css应该没问题

From my tests, many email clients simply remove all the head elements before displaying an email message. 从我的测试中,许多电子邮件客户端只需删除所有头元素,然后再显示电子邮件。 Therefore, using external style sheets isn't feasible if you have to support a wide range of email clients. 因此,如果您必须支持各种电子邮件客户端,则使用外部样式表是不可行的。

My advice is to include styling directly into the HTML, as painful as that is. 我的建议是将样式直接包含在HTML中,就像那样痛苦。

I have to say, if what you're trying to do with your script is send HTML-formatted email, even if you get the CSS files sent as attachments, it's not going to work. 我不得不说,如果您要对脚本执行的操作是发送HTML格式的电子邮件,即使您将CSS文件作为附件发送,也不会起作用。

The only way to do this with any confidence at all is to send HTML with all external files linked using absolute URLs. 完全有信心的唯一方法是发送带有使用绝对URL链接的所有外部文件的HTML。 And even then it won't work the way you expect it to. 即便如此,它也不会像你期望的那样发挥作用。

尝试使用内联css ...它的工作原理(参见http://www.tizag.com/cssT/inline.php

just came across same issue. 刚遇到同样的问题。 you may try this tool, http://templates.mailchimp.com/resources/inline-css/ remember to save the copy of original template in case you need to update 您可以尝试使用此工具, http://templates.mailchimp.com/resources/inline-css/请记住保存原始模板的副本,以备您需要更新时使用

我认为你已经得到了你的答案,但在我看来,好的方法是,不是直接输入HTML代码,而是使用一些基于html的邮件信封,在单独的html文件中包含消息和占位符,然后解析并替换占位符,以便您可以轻松地使用CSS我认为这也很容易维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM