使用 Javascript 在 Microsoft Outlook 中创建 HTML 电子邮件 [英] Use Javascript to create an HTML email in Microsoft Outlook

查看:76
本文介绍了使用 Javascript 在 Microsoft Outlook 中创建 HTML 电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 Javascript 网络应用程序创建电子邮件.我完全知道关于此的许多 SO 问题(例如 Open OutlookHTML 与 Chrome).典型答案有问题:

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:

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

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

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

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.

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

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.

创建 Outlook .MSG 文件:似乎没有库,也很少有人写到这样做.显然,该文件格式实际上嵌入了整个 FAT 文件存储系统.

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.

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

  • 确实可以访问客户端计算机,因此我可以安装帮助应用程序或加载项,根据需要更改设置等.
  • 该接口不需要实际发送邮件,它只需要为用户设置.
  • 我还需要能够向电子邮件发送来自 JS 的附件(例如 PDF).
  • I do have access to the client machines, so I could install helper applications or add-ins, change settings as needed, etc.
  • The interface does not need 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).

我不可能是第一个遇到这种情况的 Web 应用程序开发人员,但我无法找到商业或开源解决方案.

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.

更新:

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

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

var emlContent = "data:message/rfc822 eml;charset=utf-8,";
emlContent += 'To: '+emailTo+'
';
emlContent += 'Subject: '+emailSubject+'
';
emlContent += 'X-Unsent: 1'+'
';
emlContent += 'Content-Type: text/html'+'
';
emlContent += ''+'
';
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

推荐答案

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

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

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

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-Unsent MIME header to 1.

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

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

To: Joe The User <joe@domain.demo>
Subject: Test EML message
X-Unsent: 1
Content-Type: text/html

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

这篇关于使用 Javascript 在 Microsoft Outlook 中创建 HTML 电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆