简体   繁体   English

如何在 Outlook 2016+ 的电子邮件中插入 HTML(包括图像)

[英]How to insert HTML (including images) in an e-mail in Outlook 2016+

Although Outlook sends e-mails as HTML by default, Microsoft seems to want to make it hard for us to write that HTML ourselves.虽然 Outlook 默认以 HTML 发送电子邮件,但微软似乎想让我们自己编写 HTML 变得困难。 One important reason for using HTML is to keep the size of an e-mail down when inserting an image by using an <img> tag to access the image from online instead of inserting the image itself in the body of the e-mail.使用 HTML 的一个重要原因是通过使用<img>标记从在线访问图像而不是在电子邮件正文中插入图像本身,从而在插入图像时减小电子邮件的大小。

Several sources [eg, 1 , 2 ] say that the way to do this is to use "Insert as Text" to insert a file containing the HTML code.一些消息来源 [例如1 , 2 ] 说这样做的方法是使用“插入为文本”来插入包含 HTML 代码的文件。 But as of Office 2016, the "Insert as Text" option is no longer available by default.但从 Office 2016 开始,默认情况下“插入为文本”选项不再可用。 Fortunately though, there are also sources [eg, 3 ] that show how to get it back.不过幸运的是,还有一些来源 [例如3 ] 显示了如何取回它。

However, when I tried this, Outlook did not interpret my HTML.但是,当我尝试这个时,Outlook 没有解释我的 HTML。 So, for example, if I make a file containing the line:因此,例如,如果我创建一个包含以下行的文件:

<img src="https://www.lenetek.com/blog/how-to-create-html-emails-in-outlook/images/attach_file.jpg" alt="Random online image">

and then use "Insert as Text" to insert that line in my e-mail, the result is just that line of code, not the image.然后使用“Insert as Text”在我的电子邮件中插入该行,结果只是那行代码,而不是图像。 What am I doing wrong?我究竟做错了什么?

(As indicated by the alt attribute, the image file in that example is just a random online image that appears in one of the articles referenced above. I have no affiliation with that website.) (如 alt 属性所示,该示例中的图像文件只是出现在上面引用的一篇文章中的随机在线图像。我与该网站没有从属关系。)

The answer is very simple.答案很简单。 In a way, it's obvious, but in another way, it's not.在某种程度上,这是显而易见的,但在另一方面,它不是。

The answer is that each segment of HTML code inserted has to be a complete HTML file, including the <!DOCTYPE> , <HTML> and <body> tags, not just the desired HTML code.答案是插入的 HTML 代码的每一段都必须是一个完整的 HTML 文件,包括<!DOCTYPE><HTML><body>标签,而不仅仅是所需的 HTML 代码。 The reason this is not obvious is because if you insert multiple code segments, each one has to be a complete HTML file, which is something you would never do when actually writing HTML.这不明显的原因是,如果您插入多个代码段,每个代码段都必须是一个完整的 HTML 文件,这是您在实际编写 HTML 时永远不会做的事情。 I presume that what is happening is that when Outlook detects a valid HTML file being "Inserted as Text", it strips the opening and closing <!DOCTYPE> , <HTML> and <body> tags and then inserts the code that was between them -- as HTML, not as text.我认为发生的事情是,当 Outlook 检测到有效的 HTML 文件被“插入为文本”时,它会去除打开和关闭<!DOCTYPE><HTML><body>标签,然后插入它们之间的代码-- 作为 HTML,而不是文本。

So, the solution I found was that instead of the single line of code shown in the question, I need to "Insert as Text" a file containing:所以,我找到的解决方案是,我需要“插入为文本”一个包含以下内容的文件,而不是问题中显示的单行代码:

<!DOCTYPE html>
<html>
<body>
<img src="https://www.lenetek.com/blog/how-to-create-html-emails-in-outlook/images/attach_file.jpg" alt="Random online image">
</body>
</html> 

When I insert that as text in my e-mail, I see the image, not the code.当我将其作为文本插入电子邮件中时,我看到的是图像,而不是代码。

In all the sources I found online that said to use "Insert as Text" to insert HTML in Outlook, none of them said it had to be a complete HTML file instead of just the desired code.在我在网上找到的所有资源中,说使用“插入为文本”在 Outlook 中插入 HTML,没有人说它必须是完整的 HTML 文件,而不仅仅是所需的代码。 So maybe this Q&A will be helpful to someone else, if I'm not the only person who had to scratch my head for a long time before thinking of that.所以也许这个问答对其他人有帮助,如果我不是唯一一个不得不挠头很长时间才想到这一点的人。

======================== =========================

Added details about using HTML in the e-mail:在电子邮件中添加了有关使用 HTML 的详细信息:

As pointed out in the Lenetek article linked in the question, Outlook does not support all HTML tags.正如问题中链接的Lenetek 文章中所指出的,Outlook 不支持所有 HTML 标签。 In particular, for embedding images, I have found:特别是,对于嵌入图像,我发现:

When sending from Outlook: Outlook does not support <figure> and <FigCaption> .从 Outlook 发送时: Outlook 不支持<figure><FigCaption> I found that an image and caption placed in those tags were rendered inline, just ignoring the tags.我发现放置在这些标签中的图像和标题是内联渲染的,只是忽略了标签。 For floating to the right margin, I found I was able to get the same results by replacing <figure> with <table> and then placing the image and its caption each inside of <TR><TD>...</TD></TR> .对于浮动到右边距,我发现我可以通过将<figure>替换为<table>然后将图像及其标题分别放在<TR><TD>...</TD></TR>内来获得相同的结果<TR><TD>...</TD></TR>

When receiving in Outlook : There are differences in how different e-mail clients interpret HTML, which is probably why some e-mails come with a link at the top for viewing the e-mail in one's browser.在 Outlook 中接收时:不同的电子邮件客户端对 HTML 的解释方式存在差异,这可能就是为什么有些电子邮件顶部带有一个用于在浏览器中查看电子邮件的链接的原因。 In particular, I've read that Outlook is not well behaved in this regard.特别是,我读到 Outlook 在这方面表现不佳。 And that was the case with my right-floated image referred to above.上面提到的我的右浮动图像就是这种情况。

After doing the "Insert as Text" trick, the image appeared correctly at the right margin in the draft e-mail in Outlook, but when it was sent, the CSS style float attribute was ignored and the table appeared by itself at the left margin with no text wrapped around it.执行“插入为文本”技巧后,图像在 Outlook 的草稿电子邮件中正确出现在右边距,但发送时,CSS 样式的float属性被忽略,表格自行出现在左边距周围没有文字。 I was able to fix this by, in the <table> tag, replacing the style attribute float: right;我能够通过在<table>标记中替换样式属性float: right;来解决此问题。 with the old-fashioned HTML attribute align="right" .使用老式的 HTML 属性align="right" With that, the image and caption appeared correctly at the right margin when received in Outlook.这样,当在 Outlook 中接收时,图像和标题正确显示在右边距。 I have not tested what it looks like in other e-mail clients.我还没有测试过它在其他电子邮件客户端中的样子。

In addition to NewSites answer, I want to point out, that in current Outlook 365 versions the option for "Insert as text" does not appear in the standard settings.除了 NewSites 的回答,我想指出,在当前的 Outlook 365 版本中,标准设置中没有出现“插入为文本”选项。 The mentioned function under the "attach" tab does NOT offer the "Insert as text" option in the modal window. “附加”选项卡下提到的 function 在模态 window 中不提供“插入为文本”选项。

For this to work, you will have to add a new tab yourself to the ribbon and add the "Attach" button to this new tab.为此,您必须自己向功能区添加一个新选项卡,并将“附加”按钮添加到此新选项卡。 Once you click this newly added button, you will get a modal with the little dropdown next to the "Insert" button.单击这个新添加的按钮后,您将获得一个模式,其中包含“插入”按钮旁边的小下拉菜单。

I had this same issue and have been so frustrated.我遇到了同样的问题并且非常沮丧。 It's actually super easy.这实际上非常容易。 The trick is to use outlook.live.com.诀窍是使用 outlook.live.com。 Type any word in the body, highlight it, right click, select "inspect".在正文中键入任何单词,突出显示,右键单击,select“检查”。 The code will appear and the word you typed in the body of the email should be highlighted.代码将出现,您在 email 正文中键入的单词应突出显示。 Right click in the code and select "edit as html."右键单击代码和 select “编辑为 html”。 Then, in the code, highlight the word you typed in the body and replace it with your code.然后,在代码中,突出显示您在正文中键入的单词并将其替换为您的代码。 Voila: (I learned that here: https://youtu.be/yZOYRhB6ONs )瞧:(我在这里了解到: https://youtu.be/yZOYRhB6ONs

I had issues displaying the linked image while generating outlook email using HTML code.在使用 HTML 代码生成 outlook email 时显示链接图像时遇到问题。 Somehow it works on a couple of machines, but most didn't show the image.不知何故,它可以在几台机器上运行,但大多数都没有显示图像。

Kept on researching knowing the problem is with Outlook interpreting the HTML code.继续研究知道问题在于 Outlook 解释 HTML 代码。 And then I reached this thread and the @NewSites answer really nailed it.然后我到达了这个线程,@NewSites 的回答真的很到位。 Just added the line <!DOCTYPE HTML> at the top of my HTML code and all are working perfectly now.刚刚在我的 HTML 代码的顶部添加了<!DOCTYPE HTML>行,现在一切正常。

Outlook 365 (2022 Update) Outlook 365(2022 更新)

For Outlook 365, the Attach option needs to be enabled manually.对于 Outlook 365,需要手动启用Attach选项。 Modify the Command Ribbon from inside the message, not the main Outlook window:从消息内部修改命令功能区,而不是主 Outlook window:

在此处输入图像描述

Note this option is Attach File without subcommands, ie the "classic" Attach File .注意这个选项是带子命令的附加文件,即“经典”附加文件

From there you can choose Insert as Text as the HTML snippet will show up as processed, not just code.从那里您可以选择作为文本插入,因为 HTML 片段将显示为已处理,而不仅仅是代码。

Outlook version for this post: Microsoft® Outlook® for Microsoft 365 MSO (Version 2202 Build 16.0.14931.20652) 64-bit此帖子的 Outlook 版本: Microsoft® Outlook® for Microsoft 365 MSO(版本 2202 Build 16.0.14931.20652)64 位

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

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