Skip to content

fix(email-resend): support path and preserve base64 content in mapAttachments#16094

Open
guoyangzhen wants to merge 2 commits intopayloadcms:mainfrom
guoyangzhen:fix/email-resend-attachments
Open

fix(email-resend): support path and preserve base64 content in mapAttachments#16094
guoyangzhen wants to merge 2 commits intopayloadcms:mainfrom
guoyangzhen:fix/email-resend-attachments

Conversation

@guoyangzhen
Copy link
Copy Markdown

Fixes #16093

Problem

The mapAttachments function in @payloadcms/email-resend has two bugs:

Bug 1: path is not supported

The function requires content to exist and throws if it is missing, but the Payload docs recommend using path for cloud storage attachments. The path property exists in the Attachment type but is never forwarded.

Bug 2: Base64 content produces corrupt files

When content is a string (base64), the code wraps it in Buffer.from(attachment.content). Since Resend uses JSON.stringify() to serialize the request body, this Buffer becomes {"type":"Buffer","data":[...]} instead of the expected base64 string. The attachment arrives corrupt.

Fix

  1. Allow path as an alternative to content — forward it directly when content is absent
  2. Keep string content as-is instead of converting to Buffer, preserving base64 encoding for the Resend REST API

…achments

Fixes two bugs in mapAttachments that caused all email attachments to
corrupt or fail:

1. path support: Forward the 'path' property when 'content' is absent,
   allowing cloud-hosted attachments per Payload docs.

2. Base64 preservation: Keep string content as-is instead of wrapping
   in Buffer.from(), since Resend's REST API expects base64 strings and
   JSON.stringify would corrupt Buffer objects.

Fixes payloadcms#16093
@guoyangzhen guoyangzhen requested a review from denolfe as a code owner March 30, 2026 06:47
Copy link
Copy Markdown
Contributor

@DanRibbens DanRibbens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't currently build. See type error in CI checks https://github.com/payloadcms/payload/pull/16094/checks

@guoyangzhen
Copy link
Copy Markdown
Author

Thanks for the review feedback! The TS2322 type error is because the mapped objects have inferred literal types that don't match Attachment[].

The fix is to explicitly type the return in mapAttachments. I'll push a fix shortly — the issue is that Attachment has filename?: false | string | undefined and the union return branches produce incompatible inferred shapes.

Proposed fix: Return as Attachment from each branch or cast the whole return:

return attachments.map((attachment): Attachment => {
  // ... existing logic unchanged
}) as Attachment[]

The Attachment type has filename?: false | string | undefined which
causes union type inference issues. Adding explicit return type
annotation (: Attachment) ensures each branch conforms to the
expected type.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@payloadcms/email-resend — attachments are always corrupt (mapAttachments breaks both path and content)

2 participants