在创建 HTML 电子邮件时,我们应该使用 html、head、body 标签吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3903200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 04:48:43  来源:igfitidea点击:

When creating HTML emails, should we use html, head, body tags?

htmlemailhtml-email

提问by alex

In my email views, I usually just do something like...

在我的电子邮件视图中,我通常只做类似...

<dl>
   <dt>Name</dt>
   <dd>Value</dd>
</dl>

Should I be doing it like this?

我应该这样做吗?

<html>
  <head></head>
  <body>
    <dl>
       <dt>Name</dt>
       <dd>Value</dd>
    </dl>
  </body>
</html>

In other words, like I was marking up a standalone document?

换句话说,就像我在标记一个独立的文档一样?

I guess I can safely assume any web based email client will strip it out?

我想我可以放心地假设任何基于 Web 的电子邮件客户端都会将其删除?

What is the right way?

什么是正确的方法?

回答by mschonaker

The right way is to follow the HTML standard. You can validate your HTML page here.

正确的方法是遵循 HTML标准。您可以在此处验证您的 HTML 页面。

Your mail client should follow it and should throw away what's not supported or what's insecure like javascript.

您的邮件客户端应该遵循它,并且应该丢弃不支持的内容或不安全的内容,例如 javascript。

UPDATE:after several down votes from people that gets angry when you tell them to follow standards, I'll expose some reasons of why following standards could be beneficial here:

更新:在你告诉他们遵循标准时生气的人多次投反对票后,我将揭示为什么遵循标准可能有益的一些原因:

  1. a webmail willing to show your mail as a full page, could keep your format.
  2. a webmail will simply strip the tags and attributes it doesn't want. But you can't never know which ones.
  3. It's easier to find (server side) components that follow format standards, and thus less error prone. Parsers not following standards could possibly break, making your email not getting shown.
  1. 愿意将您的邮件显示为整页的网络邮件可以保留您的格式。
  2. 网络邮件将简单地去除它不想要的标签和属性。但你永远不可能知道哪些。
  3. 更容易找到遵循格式标准的(服务器端)组件,因此更不容易出错。不遵循标准的解析器可能会中断,使您的电子邮件无法显示。

回答by Josh Lee

Whether or not you include the html/head/body tags is entirely irrelevant — they are always optional and will not affect the rendering of the document in any way.

是否包含 html/head/body 标签完全无关 - 它们始终是可选的,不会以任何方式影响文档的呈现。

What matters most is whether quirks mode is on or not. Unfortunately, you can't control that in a webmail setting. Tables and inline styles are your friends. Your best bet is to test in as many webmail and desktop clients as you can.

最重要的是怪癖模式是否开启。不幸的是,您无法在网络邮件设置中控制它。表格和内联样式是您的朋友。最好的办法是在尽可能多的网络邮件和桌面客户端中进行测试。

回答by kamelkev

Many of the posts on this thread are rather old, and as a result they are no longer accurate.

该线程上的许多帖子都很旧,因此它们不再准确。

These days HTML emails should include a doctype, html and body declaration if you intend to do anything fancy at all.

现在,如果您打算做任何花哨的事情,HTML 电子邮件应该包括文档类型、html 和正文声明。

There are a multitude of guides on this subject which can help you learn how to properly code HTML Email, but most of them disregard the specifics of a doctype, which is how I stumbled on your question.

关于这个主题有很多指南可以帮助您学习如何正确编码 HTML 电子邮件,但其中大多数都忽略了文档类型的细节,这就是我偶然发现您的问题的原因。

I suggest you read the following 2 posts which are from reputable teams familiar with the various problems:

我建议您阅读以下 2 篇来自熟悉各种问题的知名团队的帖子:

campaign monitor's take

竞选监督员的看法

email on acid's take

关于酸的电子邮件

回答by Explosion Pills

Depends entirely on the email client that receives it. In my experience, most email clients that will interpret HTML don't care if you have full body/head/html tags, etc. In fact you don't even need those tags for most browsers. You need to have the head tags to include style/title, etc. Otherwise they are not really necessary, per se. I've never seen them to be necessary.

完全取决于接收它的电子邮件客户端。根据我的经验,大多数会解释 HTML 的电子邮件客户端并不关心您是否有完整的 body/head/html 标签等。事实上,对于大多数浏览器,您甚至不需要这些标签。你需要有头部标签来包含样式/标题等。否则它们本身并不是真正必要的。我从来没有看到它们是必要的。

回答by Greg

There's 1 thing I know to be true: Using HTML opening and closing tags will help in general spam scoring due to the fact that many such appliance based filters and software firewalls will add a point or so to an email that uses html but does not use the opening and closing tags.

我知道有一件事是正确的:使用 HTML 开始和结束标签将有助于一般垃圾邮件评分,因为许多此类基于设备的过滤器和软件防火墙会为使用 html 但不使用 html 的电子邮件添加一个点左右开始和结束标签。

回答by Alex Angelico

I don't think there is a right way but trying to make the email viewable in as many email readers as posible.

我认为没有正确的方法,而是试图使电子邮件在尽可能多的电子邮件阅读器中可见。

I usually check the emails in Thunderbird, because Outlook forgives more.

我通常在 Thunderbird 中查看电子邮件,因为 Outlook 可以原谅更多。

In Thunderbird this is the HTML code for an email (i have an extension that shows the html)

在 Thunderbird 中,这是电子邮件的 HTML 代码(我有一个显示 html 的扩展程序)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
<body bgcolor="#ffffff" text="#000000">
        This is the body text<br>
<div class="moz-signature"><i><br>
<br>
Regards<br>
Alex<br>
</i></div>
</body>
</html>

BTW, i use plain text email for all my web forms every time I can. I had many issues with blackberry email using html+plain text emails.

顺便说一句,我每次都使用纯文本电子邮件作为我所有的网络表单。我在使用 html+纯文本电子邮件的黑莓电子邮件方面遇到了很多问题。