Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing plaintext with inline attachments #70

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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