如何使用 CSS 构建 HTML 电子邮件?
我可以像通常构建 html 页面一样构建 html 电子邮件吗?
我想将电子邮件正文设置为所有 html 代码的字符串。
电子邮件正文当然会很复杂,所以我需要使用大量 CSS 来构建一页 html 电子邮件。
我该怎么办?
css 代码存放在哪里?
我可以只包含服务器中的外部 .css 文件吗?
Can I just build html email as usually we build html pages?
I want to set email body as a string of all html codes.
Email body offcourse will be complex, so I need to use a lot of CSS to build one page html email.
What should I do?
Where to store css codes?
Can I just include external .css file from my server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尽管这听起来完全是倒退的,而且不是我为网页推荐的,但在电子邮件中使用 css 的最佳方法是在
style
属性中内联。原因是很多电子邮件应用程序,特别是像 gmail、yahoo、hotmail 等网络邮件,不会尊重您的 css 类声明,并且通常会删除所有 css,除非它是内联的。很多时候,整个都会被删除,而且如果您将其包含在电子邮件中,
标记通常也会被删除。
如果您刚刚开始创建 HTML 电子邮件,我强烈建议您使用 Litmus 或在各种电子邮件客户端上进行手动测试。 Litmus 或其他类似的服务将为您节省大量时间。
Even though it sounds totally backwards and not what I'd recommend for web pages, the best way to use css in emails is all inline in
style
attributes. The reason is that a lot of email applications and particularly web mail like gmail, yahoo, hotmail, etc. will not respect your css class declarations and will often strip out all of your css unless it is inline. A lot of the time the entire<head>
gets removed and very often the<body>
tag is removed if you include that in your email.If you are just getting started creating HTML emails I would strongly suggest using Litmus or doing manually testing on a variety of email clients. Litmus or another similar service will save you a lot of time though.
您可以包含外部 css 文件,但它仅在网络连接时有效,最好包含内联 css。
You can include exteranal css files but it only works when net connected, better to include css inline.
将 Content-Type 标头设置为 text/html 以发送 HTML 邮件。
最好将 CSS 样式直接内联到 HTML 中。或者只是在需要样式的元素上使用“style”属性。出于安全原因,大多数邮件客户端默认不会呈现外部图像和样式。我知道您可以将图像附加到邮件中(或选择将它们放在远程服务器上 - 但需要注意的是,邮件应用程序将提示用户下载)。您也许可以将 .css 文件作为附件。
大多数发送 HTML 邮件的客户端也使用多部分/替代 MIME 格式。非 HTML 客户端如果收到这样的邮件,仍然可以将邮件呈现为纯文本。
这是一个真实的示例邮件,演示了这一点
Set the Content-Type header to text/html to send HTML mail.
You are better off inlining the CSS style directly into the HTML. Or just using the "style" attribute on the elements that need styling. Most mail clients, for security reasons, will not render external images and styles by default. I know you can attach images into the mail (or opt to have them on a remote server - but with the caveat the the mail app will prompt the user to download). You might be able to have .css files as attachments.
Most clients that send HTML mail also use the multipart/alternative MIME format. Non HTML-clients can still render the mail as plain text if they receive the mail like that.
Here is a real example mail that demonstrates this
Campaign Monitor 拥有丰富的资源,介绍 CSS 在各种电子邮件客户端中的用途和不可用之处:http:// www.campaignmonitor.com/css/
一般来说,在检查此图表后,您会发现构建 HTML/CSS 电子邮件的最佳实践与网页的最佳实践有很大不同。
Campaign Monitor has a great resource for what CSS is and isn't available in various e-mail clients: http://www.campaignmonitor.com/css/
In general after examining this chart you'll find that best practices for building HTML/CSS emails is very different than best practices for web pages.