This commit is contained in:
Andris Reinman 2019-04-15 15:14:37 +03:00
parent 664b3792a4
commit 5d5953c169

View file

@ -3,6 +3,8 @@
const config = require('wild-config');
const log = require('npmlog');
const libmime = require('libmime');
const uuid = require('uuid');
const os = require('os');
const util = require('util');
const MailComposer = require('nodemailer/lib/mail-composer');
const htmlToText = require('html-to-text');
@ -326,6 +328,20 @@ module.exports = (db, server, messageHandler, userHandler) => {
disableUrlAccess: true
};
if (data.html && typeof data.html === 'string') {
let fromAddress = (data.from && data.from.address).toString() || os.hostname();
data.html = data.html.replace(/(<img\b[^>]* src\s*=[\s"']*)(data:[^"'>\s]+)/gi, (match, prefix, dataUri) => {
let cid = uuid.v4() + '-attachments@' + fromAddress.split('@').pop();
data.attachments.push(
processDataUrl({
path: dataUri,
cid
})
);
return prefix + 'cid:' + cid;
});
}
// ensure plaintext content if html is provided
if (data.html && !data.text) {
try {
@ -471,8 +487,8 @@ module.exports = (db, server, messageHandler, userHandler) => {
[options.mailbox ? 'mailbox' : 'specialUse']: options.mailbox
? new ObjectID(options.mailbox)
: options.isDraft
? '\\Drafts'
: '\\Sent',
? '\\Drafts'
: '\\Sent',
outbound,
@ -880,3 +896,28 @@ module.exports = (db, server, messageHandler, userHandler) => {
})
);
};
function processDataUrl(element) {
let parts = (element.path || element.href).match(/^data:((?:[^;]*;)*(?:[^,]*)),(.*)$/i);
if (!parts) {
return element;
}
element.content = /\bbase64$/i.test(parts[1]) ? Buffer.from(parts[2], 'base64') : Buffer.from(decodeURIComponent(parts[2]));
if ('path' in element) {
element.path = false;
}
if ('href' in element) {
element.href = false;
}
parts[1].split(';').forEach(item => {
if (/^\w+\/[^/]+$/i.test(item)) {
element.contentType = element.contentType || item.toLowerCase();
}
});
return element;
}