Skip to content

Commit 9a16766

Browse files
authored
fix: missing plaintext with inline attachments (#70)
* fix: inline images * test: add text, html with inline attachment test
1 parent 393ab51 commit 9a16766

2 files changed

Lines changed: 47 additions & 5 deletions

File tree

src/MIMEMessage.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export class MIMEMessage {
113113

114114
let data = ''
115115

116-
if (plaintext && html && !this.hasInlineAttachments() && this.hasAttachments()) {
116+
if (plaintext && html && (this.hasInlineAttachments() || this.hasAttachments())) {
117117
data = '--' + boundary + eol +
118118
'Content-Type: multipart/alternative; boundary=' + this.boundaries.alt + eol +
119119
eol +
@@ -124,9 +124,6 @@ export class MIMEMessage {
124124
html.dump() + eol +
125125
eol +
126126
'--' + this.boundaries.alt + '--'
127-
} else if (plaintext && html && this.hasInlineAttachments()) {
128-
data = '--' + boundary + eol +
129-
html.dump()
130127
} else if (plaintext && html) {
131128
data = '--' + boundary + eol +
132129
plaintext.dump() + eol +

tests/MIMEMessage.spec.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,52 @@ test('generates plain text and html mixed message with an attachment', () => {
193193
)
194194
})
195195

196-
test('sending only an attachment, without content isn not allowed', async () => {
196+
test('generates plain text and html related message with an inline attachment', () => {
197+
const msg = new MIMEMessage(envctx)
198+
msg.boundaries = {related: 'abcdef', alt: 'qwerty'}
199+
msg.setHeader('Date', 'Wed, 22 Mar 2023 23:36:33 +0000')
200+
msg.setHeader('Message-ID', '<oliusb0xvxc@mail.com>')
201+
msg.setSender('test@mail.com')
202+
msg.setSubject('Lorem Ipsum')
203+
msg.addMessage({contentType: 'text/plain', data: 'hello there'})
204+
msg.addMessage({contentType: 'text/html', data: 'hello there <b>Murat</b>'})
205+
msg.addAttachment({
206+
inline: true,
207+
contentType: 'image/jpg',
208+
filename: 'sample.jpg',
209+
data: sampleImageBase64
210+
})
211+
212+
expect(msg.hasInlineAttachments()).toBe(true);
213+
expect(msg.getMessageByType("text/plain")?.data).toBe("hello there")
214+
215+
expect(msg.asRaw()).toBe('Date: Wed, 22 Mar 2023 23:36:33 +0000' + envctx.eol +
216+
'From: <test@mail.com>' + envctx.eol +
217+
'Message-ID: <oliusb0xvxc@mail.com>' + envctx.eol +
218+
'Subject: =?utf-8?B?TG9yZW0gSXBzdW0=?=' + envctx.eol +
219+
'MIME-Version: 1.0' + envctx.eol +
220+
'Content-Type: multipart/related; boundary=abcdef' + envctx.eol + envctx.eol +
221+
'--abcdef' + envctx.eol +
222+
'Content-Type: multipart/alternative; boundary=qwerty' + envctx.eol + envctx.eol +
223+
'--qwerty' + envctx.eol +
224+
'Content-Type: text/plain; charset=UTF-8' + envctx.eol +
225+
'Content-Transfer-Encoding: 7bit' + envctx.eol + envctx.eol +
226+
'hello there' + envctx.eol + envctx.eol +
227+
'--qwerty' + envctx.eol +
228+
'Content-Type: text/html; charset=UTF-8' + envctx.eol +
229+
'Content-Transfer-Encoding: 7bit' + envctx.eol + envctx.eol +
230+
'hello there <b>Murat</b>' + envctx.eol + envctx.eol +
231+
'--qwerty--' + envctx.eol + envctx.eol +
232+
'--abcdef' + envctx.eol +
233+
'Content-Type: image/jpg; name="sample.jpg"' + envctx.eol +
234+
'Content-Transfer-Encoding: base64' + envctx.eol +
235+
'Content-Disposition: inline; filename="sample.jpg"' + envctx.eol + envctx.eol +
236+
sampleImageBase64 + envctx.eol +
237+
'--abcdef--'
238+
)
239+
})
240+
241+
test('sending only an attachment, without content is not allowed', async () => {
197242
const msg = new MIMEMessage(envctx)
198243
msg.setHeader('Date', 'Wed, 22 Mar 2023 23:36:33 +0000')
199244
msg.setHeader('Message-ID', '<oliusb0xvxc@mail.com>')

0 commit comments

Comments
 (0)