Skip to content

Commit

Permalink
fix: missing plaintext with inline attachments (#70)
Browse files Browse the repository at this point in the history
* fix: inline images

* test: add text, html with inline attachment test
  • Loading branch information
CanRau authored Jan 2, 2025
1 parent 393ab51 commit 9a16766
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
5 changes: 1 addition & 4 deletions src/MIMEMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 +
Expand All @@ -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 +
Expand Down
47 changes: 46 additions & 1 deletion tests/MIMEMessage.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', '<[email protected]>')
msg.setSender('[email protected]')
msg.setSubject('Lorem Ipsum')
msg.addMessage({contentType: 'text/plain', data: 'hello there'})
msg.addMessage({contentType: 'text/html', data: 'hello there <b>Murat</b>'})
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: <[email protected]>' + envctx.eol +
'Message-ID: <[email protected]>' + 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 <b>Murat</b>' + 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', '<[email protected]>')
Expand Down

0 comments on commit 9a16766

Please sign in to comment.