创建MIME消息

JavaScript: create MIME message

本文关键字:消息 MIME 创建      更新时间:2023-09-26

我有发送消息的用户界面。用户输入主题、消息正文、邮件发送、附加一些文件。提交后,我需要发送消息作为MIME消息,像这样:

From: John Doe <example@example.com>
MIME-Version: 1.0
Content-Type: multipart/mixed;
        boundary="XXXXboundary text"
This is a multipart message in MIME format.
--XXXXboundary text 
Content-Type: text/plain
this is the body text
--XXXXboundary text 
Content-Type: text/plain;
Content-Disposition: attachment;
        filename="test.txt"
this is the attachment text
--XXXXboundary text--

如何收集用户输入的信息作为MIME消息?我搜索用JavaScript在客户端构建MIME消息,但没有成功。如果附件存在,我需要将它们转换为base64字符串,然后在MIME消息中发送。谢谢。

我已经创建了一个javascript插件在javascript中创建MIME消息。https://github.com/ikr0m/mime-js。创建具有必要属性的mail对象并调用createMimeMessage函数。它将MIME消息作为javascript字符串返回。

var mail = {  
    "to": "email1@example.com, email2@example.com",
    "subject": "Today is rainy",
    "fromName": "John Smith",
    "from": "john.smith@mail.com",
    "body": "Sample body text",
    "cids": [],
    "attaches" : []
}
var mimeMessage = createMimeMessage(mail);
console.log(mimeMessage);    

我认为你应该看看Node.js模块的世界…您将无法从浏览器客户端实际发送MIME邮件消息,因为没有提供SMTP支持。但是,您可以通过XHR将预格式化的消息发送到web服务器,然后让web服务器实际发送该消息。

这看起来可以做你需要的:
https://www.npmjs.org/package/mailcomposer

。您可以将MIME消息准备为字符串

这个工具可能有助于在浏览器中使用Node.js模块:
http://browserify.org/