-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathplugin.js
More file actions
45 lines (34 loc) · 1.12 KB
/
plugin.js
File metadata and controls
45 lines (34 loc) · 1.12 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
var replaceBase64Images = require('./replaceBase64Images');
var crypto = require('crypto');
var plugin = function(options) {
options = options || {};
return function(mail, done) {
if (!mail || !mail.data || !mail.data.html) {
return done();
}
mail.resolveContent(mail.data, 'html', function(err, html) {
if (err) return done(err);
var attachments = {};
html = replaceBase64Images(html, function(mimeType, base64) {
if (attachments[base64]) return attachments[base64].cid;
var randomCid = (options.cidPrefix || '') + crypto.randomBytes(8).toString('hex');
attachments[base64] = {
contentType: mimeType,
cid: randomCid,
content: base64,
encoding: 'base64',
contentDisposition: 'inline',
filename: randomCid
};
return randomCid;
});
mail.data.html = html;
if (!mail.data.attachments) mail.data.attachments = [];
Object.keys(attachments).forEach(function(cid) {
mail.data.attachments.push(attachments[cid]);
});
done();
});
}
};
module.exports = plugin;