-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMIME.js
More file actions
173 lines (147 loc) · 4.78 KB
/
Copy pathMIME.js
File metadata and controls
173 lines (147 loc) · 4.78 KB
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
172
173
var fs = require('fs');
function MIMEBase (mainType, subType) {
this.init(mainType, subType);
}
MIMEBase.prototype.init = function (mainType, subType) {
this._mainType = mainType;
this._subType = subType;
this._headers = {};
};
MIMEBase.prototype.setHeader = function (header, val, values) {
if (arguments.length == 2) {
this._headers[header] = val;
} else if (arguments.length === 3) {
this._headers[header] = val + '; ' + this._getHeaderValues(values);
}
};
MIMEBase.prototype._getContentType = function () {
return this._mainType + '/' + this._subType;
}
MIMEBase.prototype._getHeaderValues = function (values) {
var returnValues = [];
if (typeof values === 'object') {
for (var key in values) {
var value = values[key];
returnValues.push(key + '="' + value + '"');
}
}
return returnValues.join('; ');
}
MIMEBase.prototype._setContentTypeHeader = function (values) {
var contentType = this._getContentType();
this.setHeader('content-type', contentType, values);
}
MIMEBase.prototype.to = function (to) {
this.setHeader('to', to);
};
MIMEBase.prototype.from = function (from) {
this.setHeader('from', from);
};
MIMEBase.prototype.subject = function (subject) {
this.setHeader('subject', subject);
};
MIMEBase.prototype.asString = function () {
var mimeAsString = '';
for (var header in this._headers) {
var value = this._headers[header];
mimeAsString += header + ': ' + value + '\n';
}
return mimeAsString;
};
MIMEBase.prototype.asBase64url = function () {
return base64(this.asString(), null, true);
};
function MIMEMultipart () {
MIMEBase.call(this, 'multipart', 'mixed');
this._boundary = this._generateBoundary();
this._setContentTypeHeader({
boundary: this._boundary
});
this._attachments = [];
}
MIMEMultipart.prototype = Object.create(MIMEBase.prototype);
MIMEMultipart.prototype.constructor = MIMEMultipart;
MIMEMultipart.prototype._generateBoundary = function () {
return '001a11379d323968e3052a678fa4';
};
MIMEMultipart.prototype.attach = function (mime) {
if (mime instanceof MIMEBase) {
this._attachments.push(mime);
}
};
MIMEMultipart.prototype.asString = function () {
var mimeAsString = MIMEBase.prototype.asString.call(this);
mimeAsString += '\n';
var attachments = this._attachments;
for (var i = 0; i < attachments.length; i++) {
mimeAsString += '--' + this._boundary + '\n';
mimeAsString += attachments[i].asString();
}
mimeAsString += '--' + this._boundary + '--\n';
return mimeAsString;
};
function MIMEText (text, date, subType, charset) {
MIMEBase.call(this, 'text', subType || 'plain');
this._setContentTypeHeader({
charset: charset || 'us-ascii'
});
var date = new Date(date).toLocaleString('en-US', {timeZone: 'America/Los_Angeles'});
this._content = date + '\n\n' + text + '\n---';
}
MIMEText.prototype = Object.create(MIMEBase.prototype);
MIMEText.prototype.constructor = MIMEText;
MIMEText.prototype.asString = function () {
var mimeAsString = MIMEBase.prototype.asString.call(this);
mimeAsString += '\n';
mimeAsString += this._content + '\n';
return mimeAsString;
};
function MIMEImage (imageData, fileName, encoding, subType) {
MIMEBase.call(this, 'image', subType);
this.setHeader('content-disposition', 'attachment', {filename: fileName});
this.setHeader('content-transfer-encoding', 'base64');
this._encoding = encoding;
this._content = imageData;
}
MIMEImage.prototype = Object.create(MIMEBase.prototype);
MIMEImage.prototype.constructor = MIMEImage;
MIMEImage.prototype.asString = function () {
var mimeAsString = MIMEBase.prototype.asString.call(this);
mimeAsString += '\n';
mimeAsString += base64(this._content, this._encoding, false) + '\n';
return mimeAsString;
};
function base64 (data, encoding, url) {
encoding = encoding || 'ascii';
var base64data = new Buffer(data, encoding).toString('base64');
if (url) {
base64data = base64data.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, '');
}
return base64data;
}
function MIMEMultipartFromObject (obj) {
var multipart = new MIMEMultipart();
multipart.to(obj.to);
multipart.from(obj.from);
multipart.subject(obj.subject);
var text = new MIMEText(obj.content, obj.date);
multipart.attach(text);
for (var i = 0; i < obj.attachments.length; i++) {
var attachment = obj.attachments[i];
var mimeType = attachment.mimetype.split('/');
if (mimeType[0] === 'image') {
var encoding = 'binary';
var attachmentData = attachment.data;
var image = new MIMEImage(attachmentData, attachment.originalname, encoding, mimeType[1]);
multipart.attach(image);
}
}
return multipart;
}
module.exports = {
MIMEBase: MIMEBase,
MIMEMultipart: MIMEMultipart,
MIMEText: MIMEText,
MIMEImage: MIMEImage,
MIMEMultipartFromObject: MIMEMultipartFromObject
}