使用 Javascript 在 Microsoft Outlook 中创建 HTML 电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27951843/
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-23 01:00:18  来源:igfitidea点击:

Use Javascript to create an HTML email in Microsoft Outlook

javascriptweb-applicationsoutlook

提问by James Bell

I'd like to create an email from a Javascript web application. I'm completely aware of the many SO questions on this (e.g. Open Outlook HTML with Chrome). There are problems with the typical answers:

我想从 Javascript Web 应用程序创建电子邮件。我完全知道关于此的许多 SO 问题(例如Open Outlook HTML with Chrome)。典型答案存在问题:

  1. Mailto: link: This will let you create an email, but only in plain text (no HTML) and it does not allow for attachments.

  2. Activex: IE only, my application needs to run in Firefox and Chrome too. FF & Chrome plug-ins to allow ActiveX are security hazards and seem to be buggy.

  3. Server-side sends via SMTP: The email does not end up in the "Sent" folder for the user. Plus hurdles letting the user edit HTML in the browser and attach files.

  4. Create an Outlook .MSG file: There seem to be no libraries and little written about doing this. Apparently the file format actually has an entire FAT file storage system embedded.

  1. Mailto: 链接:这将允许您创建电子邮件,但仅限于纯文本(非 HTML)并且不允许附件。

  2. Activex:仅限 IE,我的应用程序也需要在 Firefox 和 Chrome 中运行。允许 ActiveX 的 FF 和 Chrome 插件存在安全隐患,而且似乎有问题。

  3. 服务器端通过 SMTP 发送:电子邮件最终不会出现在用户的“已发送”文件夹中。加上让用户在浏览器中编辑 HTML 和附加文件的障碍。

  4. 创建一个 Outlook .MSG 文件:似乎没有库,也很少写关于这样做的内容。显然,文件格式实际上嵌入了整个 FAT 文件存储系统。

Key differencesbetween many other SO questions and mine:

许多其他 SO 问题和我的问题之间的主要区别

  • I dohave access to the client machines, so I could install helper applications or add-ins, change settings as needed, etc.
  • The interface does notneed to actually send the mail, it only needs to set it up for the user.
  • I also need to be able to give the email an attachment from JS (e.g. a PDF).
  • 确实可以访问客户端计算机,因此我可以安装帮助应用程序或加载项,根据需要更改设置等。
  • 该接口并没有要实际发送邮件,只是需要将其设置为用户。
  • 我还需要能够为电子邮件提供 JS 的附件(例如 PDF)。

I cannot be the first web app developer to face this and yet I'm unable to find a solution either commercial or open source.

我不能成为第一个面临这种情况的 Web 应用程序开发人员,但我无法找到商业或开源解决方案。

Update:

更新:

I used the EML file method and it works well so far. Here's my JS code to create and trigger it:

我使用了 EML 文件方法,到目前为止效果很好。这是我创建和触发它的 JS 代码:

var emlContent = "data:message/rfc822 eml;charset=utf-8,";
emlContent += 'To: '+emailTo+'\n';
emlContent += 'Subject: '+emailSubject+'\n';
emlContent += 'X-Unsent: 1'+'\n';
emlContent += 'Content-Type: text/html'+'\n';
emlContent += ''+'\n';
emlContent += htmlDocument;

var encodedUri = encodeURI(emlContent); //encode spaces etc like a url
var a = document.createElement('a'); //make a link in document
var linkText = document.createTextNode("fileLink");
a.appendChild(linkText);
a.href = encodedUri;
a.id = 'fileLink';
a.download = 'filename.eml';
a.style = "display:none;"; //hidden link
document.body.appendChild(a);
document.getElementById('fileLink').click(); //click the link

采纳答案by Dmitry Streblechenko

MSG file format is documented, but it is certainly not fun... Why not create an EML (MIME) file?

MSG 文件格式已记录,但它肯定不好玩...为什么不创建一个 EML (MIME) 文件?

To those who want to delete or downvote this answer: the suggestion is to use the EML (MIME) format. According to the OP, he considered the MSG file format (#4), but was discouraged due to complexity or lack of JS libraries that process that format. If MSG file was considered, MIME is a much better choice - it is text based, so no special libraries are required to create it. Outlook will be able to open it just as easily as an MSG file. To make sure it is treated as an unsent message by Outlook, set the X-UnsentMIME header to 1.

对于那些想要删除或否决此答案的人:建议使用 EML (MIME) 格式。根据 OP,他考虑了 MSG 文件格式(#4),但由于复杂性或缺乏处理该格式的 JS 库而被劝阻。如果考虑 MSG 文件,MIME 是一个更好的选择 - 它是基于文本的,因此不需要特殊的库来创建它。Outlook 将能够像打开 MSG 文件一样轻松地打开它。要确保 Outlook 将其视为未发送的邮件,请将X-UnsentMIME 标头设置为 1。

UPDATE: The simplest EML file would look like the following:

更新:最简单的 EML 文件如下所示:

To: Joe The User <[email protected]>
Subject: Test EML message
X-Unsent: 1
Content-Type: text/html

<html>
<body>
Test message with <b>bold</b> text.
</body>
</html>

回答by CalvT

Using the idea of plain text eml files, I came up with this: http://jsfiddle.net/CalvT/un3hapej/

使用纯文本 eml 文件的想法,我想出了这个:http: //jsfiddle.net/CalvT/un3hapej/

This is an edit of something I found - to create a .txtfile then download it. As .emlfiles are practically .txt files, I figured this would work. And it does. I've left the textareawith the sample email in so you can easily test. When you click on create file, it then gives you a download link to download your .emlfile. The only hurdle I can see is making the browser open the .emlfile after it has been downloaded.

这是我发现的东西的编辑 - 创建一个.txt文件然后下载它。由于.eml文件实际上是 .txt 文件,我认为这会起作用。确实如此。我已经留下了textarea示例电子邮件,以便您可以轻松测试。当您单击创建文件时,它会为您提供一个下载链接以下载您的.eml文件。我能看到的唯一障碍是让浏览器.eml在下载文件后打开它。

EDIT: And thinking about it, as you have access to the client machines, you could set the browser to always open files of that type. For instance in Chrome, you can click on the arrow beside the download and select always open files of this type.

编辑:考虑一下,当您可以访问客户端计算机时,您可以将浏览器设置为始终打开该类型的文件。例如,在 Chrome 中,您可以单击下载旁边的箭头并选择始终打开此类型的文件。

Here's the code

这是代码

HTML:

HTML:

(function () {
var textFile = null,
  makeTextFile = function (text) {
    var data = new Blob([text], {type: 'text/plain'});

    if (textFile !== null) {
      window.URL.revokeObjectURL(textFile);
    }

    textFile = window.URL.createObjectURL(data);

    return textFile;
  };


  var create = document.getElementById('create'),
    textbox = document.getElementById('textbox');

  create.addEventListener('click', function () {
    var link = document.getElementById('downloadlink');
    link.href = makeTextFile(textbox.value);
    link.style.display = 'block';
  }, false);
})();
<textarea id="textbox" style="width: 300px; height: 200px;">
To: User <[email protected]>
Subject: Subject
X-Unsent: 1
Content-Type: text/html

<html>
<body>
Test message
</body>
</html>
  
</textarea>

<button id="create">Create file</button>
  
<a download="message.eml" id="downloadlink" style="display: none">Download</a>

回答by korhojoa

Nobody seems to have answered the attachment question, so here's my solution: create the EML as a multipart/mixed message.

似乎没有人回答附件问题,所以这是我的解决方案:将 EML 创建为多部分/混合消息。

Content-Type: multipart/mixed; boundary=--boundary_text_string

With this, you can have multiple parts in your email. Multiple parts let you add attachments, like this.

有了这个,您的电子邮件中可以有多个部分。多个部分可让您添加附件,就像这样。

Content-Type: application/octet-stream; name=demo.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment

Start with your email headers, then add your boundary, then the part contents (newline locations are very important, clients won't parse your file correctly otherwise). You can add multiple parts. Below is an example. Note that the last boundary is different from the others (2 dashes at the end).

从您的电子邮件标题开始,然后添加您的边界,然后是部分内容(换行位置非常重要,否则客户端将无法正确解析您的文件)。您可以添加多个部分。下面是一个例子。请注意,最后一个边界与其他边界不同(末尾有 2 个破折号)。

To: Demo-Recipient <[email protected]>
Subject: EML with attachments
X-Unsent: 1
Content-Type: multipart/mixed; boundary=--boundary_text_string

----boundary_text_string
Content-Type: text/html; charset=UTF-8

<html>
<body>
<p>Example</p>
</body>
</html>

----boundary_text_string
Content-Type: application/octet-stream; name=demo.txt
Content-Transfer-Encoding: base64
Content-Disposition: attachment
ZXhhbXBsZQ==

----boundary_text_string
Content-Type: application/octet-stream; name=demo.log
Content-Transfer-Encoding: base64
Content-Disposition: attachment
ZXhhbXBsZQ==

----boundary_text_string--

This gives you a eml file with two attachments. See RFC 1371if you want to know more specifics on how this works.

这为您提供了一个带有两个附件的 eml 文件。如果您想了解有关其工作原理的更多细节,请参阅RFC 1371