【发布时间】:2019-06-04 19:28:15
【问题描述】:
我正在通过 JavaScript 在我的网站上创建一个 eml 文件作为 text/html 类型,供我的用户下载并用作模板。我想在此 eml 文件中添加附件,以便在下载附件时自动将附件附加到电子邮件中。在问这个问题之前,我对这个问题进行了很多研究,但我找不到合适的答案/解决方案。所以我的问题是,我怎么可能通过 JavaScript 向 eml 文件添加附件?
我尝试添加指向 eml 文件的链接,即通过指向附件的链接,该附件与我的编码文件位于同一文件夹中。但是,当我在发送前后单击 Outlook 中的链接时,我收到一条错误消息,提示无法找到该文件。
var emailTo, emailSubject, htmlDocumentContent;
emailSubject = "User Account Created";
emailTo = "xxx@example.com";
htmlDocumentContent = "html code goes here";
//creating the eml file
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 += htmlDocumentContent;
var encodedUri = encodeURI(emlContent); //encode spaces etc like a url
var a = document.createElement('a'); //create a link element in the document
var linkText = document.createTextNode("fileLink"); //create the file's link to the element
a.appendChild(linkText); //append the file's link to the element
a.href = encodedUri;
a.id = 'fileLink';
a.download = 'JDE New User Email.eml';
a.style = "display:none;"; //hidden link
document.body.appendChild(a);
document.getElementById('fileLink').click(); //click the link
我要添加到此代码或此 eml 文件的内容是一个 pdf 附件,因此当用户下载它时,它会自动附加 pdf 文件。
非常感谢您提前提供的帮助。
【问题讨论】:
-
至少将此问题标记为已回答
标签: javascript email-attachments eml