From b1ccba26c0eebd6f4c4bd3df5fac96a904fa6817 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0mundur=20Gunnarsson?= Date: Thu, 3 Aug 2017 13:41:05 +0000 Subject: [PATCH] Added isEncoded option to createChild which prevents build from encoding node content. --- src/builder-unit.js | 17 +++++++++++++++++ src/builder.js | 38 ++++++++++++++++++++++++-------------- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/src/builder-unit.js b/src/builder-unit.js index 598acf8..056a35f 100644 --- a/src/builder-unit.js +++ b/src/builder-unit.js @@ -507,6 +507,23 @@ describe('Mimebuilder', function () { expect(mb.build()).to.equal(expected) }) + it('should not encode if isEncoded is true', function () { + const mb = new Mimebuilder('multipart/mixed') + .createChild('image/jpeg', { + filename: 'encoded.jpg', + isEncoded: true + }) + .setContent('this should not change') + const expected = + 'Content-Type: image/jpeg\r\n' + + 'Content-Transfer-Encoding: base64\r\n' + + 'Content-Disposition: attachment; filename=encoded.jpg\r\n' + + '\r\n' + + 'this should not change' + + expect(mb.build()).to.equal(expected) + }) + it('should use from domain for message-id', function () { const mb = new Mimebuilder('text/plain') .setHeader({ diff --git a/src/builder.js b/src/builder.js index bded57d..c4c8d6f 100644 --- a/src/builder.js +++ b/src/builder.js @@ -26,6 +26,7 @@ import { * @param {Object} [options.parentNode] immediate parent for this node * @param {Object} [options.filename] filename for an attachment node * @param {String} [options.baseBoundary] shared part of the unique multipart boundary + * @param {String} [options.isEncoded] is the node's content encoded or not */ export default class MimeNode { constructor (contentType, options = {}) { @@ -91,6 +92,11 @@ export default class MimeNode { * If true then BCC header is included in RFC2822 message. */ this.includeBccInHeader = options.includeBccInHeader || false + + /** + * Is this node's content already encoded + */ + this.isEncoded = options.isEncoded } /** @@ -400,20 +406,24 @@ export default class MimeNode { lines.push('') if (this.content) { - switch (transferEncoding) { - case 'quoted-printable': - lines.push(quotedPrintableEncode(this.content)) - break - case 'base64': - lines.push(base64Encode(this.content, typeof this.content === 'object' ? 'binary' : undefined)) - break - default: - if (flowed) { - // space stuffing http://tools.ietf.org/html/rfc3676#section-4.2 - lines.push(foldLines(this.content.replace(/\r?\n/g, '\r\n').replace(/^( |From|>)/igm, ' $1'), 76, true)) - } else { - lines.push(this.content.replace(/\r?\n/g, '\r\n')) - } + if (this.isEncoded) { + lines.push(this.content.replace(/\r?\n/g, '\r\n')) + } else { + switch (transferEncoding) { + case 'quoted-printable': + lines.push(quotedPrintableEncode(this.content)) + break + case 'base64': + lines.push(base64Encode(this.content, typeof this.content === 'object' ? 'binary' : undefined)) + break + default: + if (flowed) { + // space stuffing http://tools.ietf.org/html/rfc3676#section-4.2 + lines.push(foldLines(this.content.replace(/\r?\n/g, '\r\n').replace(/^( |From|>)/igm, ' $1'), 76, true)) + } else { + lines.push(this.content.replace(/\r?\n/g, '\r\n')) + } + } } if (this.multipart) { lines.push('')