From 4270b767ee4676391fd6c15659095d84c8cde12e Mon Sep 17 00:00:00 2001 From: CanRau Date: Sun, 19 May 2024 17:14:42 -0500 Subject: [PATCH 1/2] fix: inline images --- src/MIMEMessage.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/MIMEMessage.ts b/src/MIMEMessage.ts index 7c9eec1..3e8ec55 100644 --- a/src/MIMEMessage.ts +++ b/src/MIMEMessage.ts @@ -113,7 +113,7 @@ export class MIMEMessage { let data = '' - if (plaintext && html && !this.hasInlineAttachments() && this.hasAttachments()) { + if (plaintext && html && (this.hasInlineAttachments() || this.hasAttachments())) { data = '--' + boundary + eol + 'Content-Type: multipart/alternative; boundary=' + this.boundaries.alt + eol + eol + @@ -124,9 +124,6 @@ export class MIMEMessage { html.dump() + eol + eol + '--' + this.boundaries.alt + '--' - } else if (plaintext && html && this.hasInlineAttachments()) { - data = '--' + boundary + eol + - html.dump() } else if (plaintext && html) { data = '--' + boundary + eol + plaintext.dump() + eol + From 95550a88ca19bed204c026a0a9f75ad223e3079d Mon Sep 17 00:00:00 2001 From: CanRau Date: Sun, 19 May 2024 17:36:43 -0500 Subject: [PATCH 2/2] test: add text, html with inline attachment test --- tests/MIMEMessage.spec.js | 47 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/tests/MIMEMessage.spec.js b/tests/MIMEMessage.spec.js index d30f14c..6eb5017 100644 --- a/tests/MIMEMessage.spec.js +++ b/tests/MIMEMessage.spec.js @@ -193,7 +193,52 @@ test('generates plain text and html mixed message with an attachment', () => { ) }) -test('sending only an attachment, without content isn not allowed', async () => { +test('generates plain text and html related message with an inline attachment', () => { + const msg = new MIMEMessage(envctx) + msg.boundaries = {related: 'abcdef', alt: 'qwerty'} + msg.setHeader('Date', 'Wed, 22 Mar 2023 23:36:33 +0000') + msg.setHeader('Message-ID', '') + msg.setSender('test@mail.com') + msg.setSubject('Lorem Ipsum') + msg.addMessage({contentType: 'text/plain', data: 'hello there'}) + msg.addMessage({contentType: 'text/html', data: 'hello there Murat'}) + msg.addAttachment({ + inline: true, + contentType: 'image/jpg', + filename: 'sample.jpg', + data: sampleImageBase64 + }) + + expect(msg.hasInlineAttachments()).toBe(true); + expect(msg.getMessageByType("text/plain")?.data).toBe("hello there") + + expect(msg.asRaw()).toBe('Date: Wed, 22 Mar 2023 23:36:33 +0000' + envctx.eol + + 'From: ' + envctx.eol + + 'Message-ID: ' + envctx.eol + + 'Subject: =?utf-8?B?TG9yZW0gSXBzdW0=?=' + envctx.eol + + 'MIME-Version: 1.0' + envctx.eol + + 'Content-Type: multipart/related; boundary=abcdef' + envctx.eol + envctx.eol + + '--abcdef' + envctx.eol + + 'Content-Type: multipart/alternative; boundary=qwerty' + envctx.eol + envctx.eol + + '--qwerty' + envctx.eol + + 'Content-Type: text/plain; charset=UTF-8' + envctx.eol + + 'Content-Transfer-Encoding: 7bit' + envctx.eol + envctx.eol + + 'hello there' + envctx.eol + envctx.eol + + '--qwerty' + envctx.eol + + 'Content-Type: text/html; charset=UTF-8' + envctx.eol + + 'Content-Transfer-Encoding: 7bit' + envctx.eol + envctx.eol + + 'hello there Murat' + envctx.eol + envctx.eol + + '--qwerty--' + envctx.eol + envctx.eol + + '--abcdef' + envctx.eol + + 'Content-Type: image/jpg; name="sample.jpg"' + envctx.eol + + 'Content-Transfer-Encoding: base64' + envctx.eol + + 'Content-Disposition: inline; filename="sample.jpg"' + envctx.eol + envctx.eol + + sampleImageBase64 + envctx.eol + + '--abcdef--' + ) +}) + +test('sending only an attachment, without content is not allowed', async () => { const msg = new MIMEMessage(envctx) msg.setHeader('Date', 'Wed, 22 Mar 2023 23:36:33 +0000') msg.setHeader('Message-ID', '')