Skip to content

Commit b1ccba2

Browse files
Guðmundur Gunnarssonnifgraup
authored andcommitted
Added isEncoded option to createChild which prevents build from encoding node content.
1 parent 7090355 commit b1ccba2

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

src/builder-unit.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,23 @@ describe('Mimebuilder', function () {
507507
expect(mb.build()).to.equal(expected)
508508
})
509509

510+
it('should not encode if isEncoded is true', function () {
511+
const mb = new Mimebuilder('multipart/mixed')
512+
.createChild('image/jpeg', {
513+
filename: 'encoded.jpg',
514+
isEncoded: true
515+
})
516+
.setContent('this should not change')
517+
const expected =
518+
'Content-Type: image/jpeg\r\n' +
519+
'Content-Transfer-Encoding: base64\r\n' +
520+
'Content-Disposition: attachment; filename=encoded.jpg\r\n' +
521+
'\r\n' +
522+
'this should not change'
523+
524+
expect(mb.build()).to.equal(expected)
525+
})
526+
510527
it('should use from domain for message-id', function () {
511528
const mb = new Mimebuilder('text/plain')
512529
.setHeader({

src/builder.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
* @param {Object} [options.parentNode] immediate parent for this node
2727
* @param {Object} [options.filename] filename for an attachment node
2828
* @param {String} [options.baseBoundary] shared part of the unique multipart boundary
29+
* @param {String} [options.isEncoded] is the node's content encoded or not
2930
*/
3031
export default class MimeNode {
3132
constructor (contentType, options = {}) {
@@ -91,6 +92,11 @@ export default class MimeNode {
9192
* If true then BCC header is included in RFC2822 message.
9293
*/
9394
this.includeBccInHeader = options.includeBccInHeader || false
95+
96+
/**
97+
* Is this node's content already encoded
98+
*/
99+
this.isEncoded = options.isEncoded
94100
}
95101

96102
/**
@@ -400,20 +406,24 @@ export default class MimeNode {
400406
lines.push('')
401407

402408
if (this.content) {
403-
switch (transferEncoding) {
404-
case 'quoted-printable':
405-
lines.push(quotedPrintableEncode(this.content))
406-
break
407-
case 'base64':
408-
lines.push(base64Encode(this.content, typeof this.content === 'object' ? 'binary' : undefined))
409-
break
410-
default:
411-
if (flowed) {
412-
// space stuffing http://tools.ietf.org/html/rfc3676#section-4.2
413-
lines.push(foldLines(this.content.replace(/\r?\n/g, '\r\n').replace(/^( |From|>)/igm, ' $1'), 76, true))
414-
} else {
415-
lines.push(this.content.replace(/\r?\n/g, '\r\n'))
416-
}
409+
if (this.isEncoded) {
410+
lines.push(this.content.replace(/\r?\n/g, '\r\n'))
411+
} else {
412+
switch (transferEncoding) {
413+
case 'quoted-printable':
414+
lines.push(quotedPrintableEncode(this.content))
415+
break
416+
case 'base64':
417+
lines.push(base64Encode(this.content, typeof this.content === 'object' ? 'binary' : undefined))
418+
break
419+
default:
420+
if (flowed) {
421+
// space stuffing http://tools.ietf.org/html/rfc3676#section-4.2
422+
lines.push(foldLines(this.content.replace(/\r?\n/g, '\r\n').replace(/^( |From|>)/igm, ' $1'), 76, true))
423+
} else {
424+
lines.push(this.content.replace(/\r?\n/g, '\r\n'))
425+
}
426+
}
417427
}
418428
if (this.multipart) {
419429
lines.push('')

0 commit comments

Comments
 (0)