Skip to content

fix(emails): send binary attachment content as base64 - #1167

Merged
dielduarte merged 2 commits into
resend:canaryfrom
yassinemontassar:fix/attachment-binary-content
Oct 30, 2026
Merged

dielduarte merged 2 commits into
resend:canaryfrom
yassinemontassar:fix/attachment-binary-content

Conversation

@yassinemontassar

@yassinemontassar yassinemontassar commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Binary attachment content (Buffer, Uint8Array, ArrayBuffer) is now sent as base64, and the content type accepts Uint8Array | ArrayBuffer.

Why

parseAttachments passes content straight to JSON.stringify, which only really works for strings:

  • ArrayBuffer becomes {}, so the API says the attachment content is empty (same thing as Attachment doesn't work with Cloudflare R2 #543)
  • Uint8Array becomes {"0":37,"1":80,...}, rejected with 422 invalid_attachment: Attachment content must be a base64-encoded string.
  • Buffer becomes {"type":"Buffer","data":[...]}, which works but is ~2.7x bigger than base64

I ran into this generating PDFs with pdf-lib, which returns a Uint8Array. Same goes for fetch().arrayBuffer() or R2. The workaround from #543 (Buffer.from(...)) works on Node but needs Buffer, which isn't there on every edge runtime, and the error doesn't really point you to the cause.

Change

Binary content gets base64-encoded in parseAttachments, using Buffer when it exists and btoa (chunked) otherwise. Typed-array views respect byteOffset/byteLength. Strings are passed through as before. Since parseAttachments is shared, drafts and inbox thread replies/forwards get this too.

Testing

  • Added tests for each binary type, offset views, empty content, the btoa fallback with Buffer stubbed out, mixed attachments, a different PDF per email, and drafts. 9 of them fail on canary.
  • pnpm test, typecheck, lint and build all pass.
  • Checked against the live API with a 3-page PDF. On canary the Uint8Array send gets the 422 above. With this change, all three types are accepted and the PDFs come through intact.

Compatibility

Types only get wider, and string content is unchanged. The one behavior change is that Buffer content is now sent as base64 instead of { type, data }. I tested that against the API and it works, but if you'd rather keep Buffer as it was, I'm happy to limit the encoding to Uint8Array/ArrayBuffer.

@yassinemontassar
yassinemontassar requested a review from a team as a code owner October 17, 2026 20:03
@github-actions github-actions Bot added the linear-synced PR has been synced to Linear label Oct 18, 2026
JSON.stringify turns a Uint8Array into an object of indexes and an ArrayBuffer into {}, so those attachments were rejected or arrived empty (resend#543). Binary content is now sent as base64, and the type accepts Uint8Array and ArrayBuffer.
@dielduarte
dielduarte requested review from dielduarte and removed request for alecventura October 30, 2026 21:41
@dielduarte
dielduarte force-pushed the fix/attachment-binary-content branch from 31cee31 to 2beed8d Compare October 30, 2026 21:42
@pkg-pr-new

pkg-pr-new Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/resend@1167

commit: aa082eb

@dielduarte
dielduarte merged commit 5265fee into resend:canary Oct 30, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

linear-synced PR has been synced to Linear

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants