Skip to content
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
11 changes: 5 additions & 6 deletions src/transports/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,13 @@ class NodeMailerTransport implements Transport {
}

/**
* Formatting recipients to the "Name <email>" string format. Cloudflare
* accepts both plain strings and `{ email, name }` objects, but its docs
* are inconsistent on the object key (`email` vs `address`), so we use
* the universally accepted string representation.
* Formatting recipients for the Cloudflare API. A named recipient is sent
* as an `{ address, name }` object, since a plain string must be printable
* ASCII and "Name <email>" fails for names like "Γ‰quipe RH".
*/
#formatRecipients(
recipients?: MailMessage['data']['to'] | MailMessage['data']['cc'] | MailMessage['data']['bcc']
): string[] {
): (string | { address: string; name: string })[] {
if (!recipients) {
return []
}
Expand All @@ -74,7 +73,7 @@ class NodeMailerTransport implements Transport {
}

if (recipient.name) {
return `${recipient.name} <${recipient.address}>`
return { address: recipient.address, name: recipient.name }
}

return recipient.address
Expand Down
28 changes: 24 additions & 4 deletions tests/unit/transports/cloudflare.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ test.group('Cloudflare transport | payload', () => {
assert.equal(headers.authorization, 'Bearer token')
})

test('format recipients as "Name <email>" strings', async ({ assert }) => {
test('format named recipients as address objects', async ({ assert }) => {
const { server, ready, payload } = captureServer(SUCCESS_RESPONSE)
const baseUrl = await ready

Expand All @@ -75,11 +75,31 @@ test.group('Cloudflare transport | payload', () => {
server.close()

const body = await payload
assert.equal(body.from, 'Sender <sender@example.com>')
assert.deepEqual(body.to, ['Receiver <receiver@example.com>'])
assert.deepEqual(body.from, { address: 'sender@example.com', name: 'Sender' })
assert.deepEqual(body.to, [{ address: 'receiver@example.com', name: 'Receiver' }])
assert.deepEqual(body.cc, ['cc@example.com'])
assert.deepEqual(body.bcc, ['bcc@example.com'])
assert.equal(body.reply_to, 'Reply <reply@example.com>')
assert.deepEqual(body.reply_to, { address: 'reply@example.com', name: 'Reply' })
})

test('send names with non-ASCII characters', async ({ assert }) => {
const { server, ready, payload } = captureServer(SUCCESS_RESPONSE)
const baseUrl = await ready

const message = new Message()
message.from('noreply@example.com', 'Γ‰quipe RH')
message.to('receiver@example.com')
message.subject('Hello')
message.html('<p>Hello</p>')

await new CloudflareTransport({ key: 'token', baseUrl, accountId: 'acc' }).send(
message.toJSON().message
)
server.close()

const body = await payload
assert.deepEqual(body.from, { address: 'noreply@example.com', name: 'Γ‰quipe RH' })
assert.deepEqual(body.to, ['receiver@example.com'])
})

test('forward custom and list headers', async ({ assert }) => {
Expand Down