-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple-mailparser-javascript.js
171 lines (150 loc) · 4.58 KB
/
simple-mailparser-javascript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const regexBoundary = /boundary=(.*)/g;
const regexContentTypeWithoutBoundary = /Content-Type:\s([a-zA-Z0-9\/=+]+);\r/g;
const regexContentType = /Content-Type:\s([a-zA-Z0-9\/=+]+);\scharset=(.*)/g;
const regexContentTransferEncoding = /Content-Transfer-Encoding:\s(.*)/g;
const regexContentTypeAttachment = /Content-Type:\s([a-zA-z\/]+);\sname="(.*)"/g;
const regexContentDisposition = /Content-Disposition:\s([a-zA-z\/]+);\sfilename="(.*)"/g;
const regexContentAttachmentTransferEncoding = /Content-Transfer-Encoding:\s(.*)/g;
const regexContentId = /Content-ID:\s(.*)/g;
const regexXAttachmentId = /X-Attachment-Id:\s(.*)/g;
function codeline_MailParser(data) {
var parsing = false;
var charsetText = "";
var dataText = "";
var charsetHtml = "";
var dataHtml = "";
var boundary = "";
var boundaries = [];
var attachments = []
var rawMessage = [];
var tmp_ContentTypeAttachment = "";
var tmp_ContentDisposition = "";
var tmp_ContentAttachmentTransferEncoding = "";
var tmp_ContentId = "";
var tmp_XAttachmentId = "";
var tmp_filename = "";
var tmp_data = "";
data.toString().split("\n").forEach(function(line) {
if(line.match(regexContentTypeWithoutBoundary)) {
rawMessage.push(line);
return;
}
if(line.match(regexBoundary)) {
boundaryTmp = regexBoundary.exec(line);
boundary = boundaryTmp[1].replace(/\"/g, '');
console.log(boundary);
boundaries.push("--" + boundary+"\r");
boundaries.push("--" + boundary + "--\r");
rawMessage.push(line);
return;
}
if(boundaries.indexOf(line) > -1) {
if(line.substr(line.length-3, 2) == "--") {
if(tmp_data != "") {
newAttachment = {};
newAttachment.ContentType = tmp_ContentTypeAttachment;
newAttachment.ContentDisposition = tmp_ContentDisposition;
newAttachment.ContentTransferEncoding = tmp_ContentAttachmentTransferEncoding;
newAttachment.ContentId = tmp_ContentId;
newAttachment.XAttachmentId = tmp_XAttachmentId;
newAttachment.name = tmp_filename;
newAttachment.data = tmp_data;
attachments.push(newAttachment);
tmp_ContentTypeAttachment = "";
tmp_ContentDisposition = "";
tmp_ContentAttachmentTransferEncoding = "";
tmp_ContentId = "";
tmp_XAttachmentId = "";
tmp_filename = "";
tmp_data = "";
}
parsing = false;
} else {
parsing = true;
}
rawMessage.push(line);
return;
}
if(rawMessage.length > 0) rawMessage.push(line);
if(parsing) {
if(line.match(regexContentType)) {
regEx = regexContentType.exec(line);
parsing = regEx[1];
if(parsing == "text/plain") charsetText = regEx[2];
if(parsing == "text/html") charsetHtml = regEx[2];
return;
}
if(parsing == "text/plain") {
if(line.match(regexContentAttachmentTransferEncoding)) {
return;
}
dataText += line + "\n";
} else if(parsing == "text/html") {
if(line.match(regexContentAttachmentTransferEncoding)) {
return;
}
dataHtml += line + "\n" ;
} else {
if(line != "\r") {
if(line.match(regexContentTypeAttachment)) {
tmp = regexContentTypeAttachment.exec(line);
tmp_ContentTypeAttachment = tmp[1];
tmp_filename = tmp[2];
return;
}
if(line.match(regexContentDisposition)) {
tmp = regexContentDisposition.exec(line);
tmp_ContentDisposition = tmp[1];
tmp_filename = tmp[2];
return;
}
if(line.match(regexContentAttachmentTransferEncoding)) {
tmp = regexContentAttachmentTransferEncoding.exec(line);
tmp_ContentAttachmentTransferEncoding = tmp[1];
return;
}
if(line.match(regexContentId)) {
tmp = regexContentId.exec(line);
tmp_ContentId = tmp[1];
return;
}
if(line.match(regexXAttachmentId)) {
tmp = regexXAttachmentId.exec(line);
tmp_XAttachmentId = tmp[1];
return;
}
tmp_data += line.replace("\r", "");
}
}
}
});
rawContentType = "";
rawContentTransferEncoding = "";
parsing = false;
if(rawMessage.length == 0) {
data.toString().split("\n").forEach(function(line) {
if(line.match(regexContentType)) {
rawContentType = line.replace("\r", "");
return;
}
if(line.match(regexContentAttachmentTransferEncoding)) {
rawContentTransferEncoding = line.replace("\r", "");
return;
}
if(line == "\r") parsing = true;
if(parsing) {
rawMessage.push(line);
}
});
}
return {
text: dataText,
textCharset: charsetText,
html: dataHtml,
charsetHtml: charsetHtml,
attachments: attachments,
rawContentType: rawContentType,
rawContentTransferEncoding: rawContentTransferEncoding,
rawMessage: rawMessage.join("\n")
};
}