-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraw.js
More file actions
97 lines (78 loc) · 2.38 KB
/
raw.js
File metadata and controls
97 lines (78 loc) · 2.38 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
'use strict';
const MaxLine = function (data) {
let c = 0, result = '';
for (let i = 0, l = data.length; i < l; i++) {
result += data[i];
if (c === 74) {
result += '\n';
c = 0;
} else {
c++;
}
}
return result;
};
module.exports = function (data) {
const { cc, bcc, to, from, text, html, reply, subject, attachments } = data || {};
const MIXED_BOUNDARY = 'MIXED_BOUNDARY';
const ALTERNATIVE_BOUNDARY = 'ALTERNATIVE_BOUNDARY';
const BOUNDARY = text && html ? ALTERNATIVE_BOUNDARY : MIXED_BOUNDARY;
const raw = [
`From: ${from}`,
`To: ${to.join(',')}`
];
if (reply) {
raw.push(`Reply-To: ${reply.join(',')}`);
}
if (cc) {
raw.push(`CC: ${cc.join(',')}`);
}
if (bcc) {
raw.push(`BCC: ${bcc.join(',')}`);
}
raw.push(
`Subject: ${subject}`,
`Content-Type: multipart/mixed;`,
`\tboundary="${MIXED_BOUNDARY}"`
);
if (text && html) {
raw.push(
`\n--${MIXED_BOUNDARY}`,
`Content-Type: multipart/alternative;`,
`\tboundary="${ALTERNATIVE_BOUNDARY}"`
);
}
if (text) {
raw.push(
`\n--${BOUNDARY}`,
`Content-Transfer-Encoding: base64`,
`Content-Type: text/plain; charset=utf-8\n`,
MaxLine(Buffer.from(text).toString('base64'))
);
}
if (html) {
raw.push(
`\n--${BOUNDARY}`,
`Content-Transfer-Encoding: base64`,
`Content-Type: text/html; charset=utf-8\n`,
MaxLine(Buffer.from(html).toString('base64'))
);
}
if (text && html) {
raw.push(`\n--${BOUNDARY}--`);
}
if (attachments) {
for (const attachment of attachments) {
raw.push(
`\n--${MIXED_BOUNDARY}`,
`Content-Transfer-Encoding: base64`,
`Content-Description: ${attachment.name}`,
`Content-Disposition: attachment; filename="${attachment.name}"`,
`Content-Type: text/plain; charset=${attachment.encoding || 'utf-8'}; name="${attachment.name}"\n`,
MaxLine(Buffer.from(attachment.data, attachment.encoding || 'utf8').toString('base64'))
);
}
}
raw.push(`\n--${MIXED_BOUNDARY}--`);
return raw.join('\n');
};