Repository navigation
fix(emails): send binary attachment content as base64 - #1167
Merged
dielduarte merged 2 commits intoOct 30, 2026
Merged
Conversation
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
requested review from
dielduarte
and removed request for
alecventura
October 30, 2026 21:41
dielduarte
force-pushed
the
fix/attachment-binary-content
branch
from
October 30, 2026 21:42
31cee31 to
2beed8d
Compare
commit: |
dielduarte
approved these changes
Oct 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Binary attachment content (
Buffer,Uint8Array,ArrayBuffer) is now sent as base64, and thecontenttype acceptsUint8Array | ArrayBuffer.Why
parseAttachmentspassescontentstraight toJSON.stringify, which only really works for strings:ArrayBufferbecomes{}, so the API says the attachment content is empty (same thing as Attachment doesn't work with Cloudflare R2 #543)Uint8Arraybecomes{"0":37,"1":80,...}, rejected with422 invalid_attachment: Attachment content must be a base64-encoded string.Bufferbecomes{"type":"Buffer","data":[...]}, which works but is ~2.7x bigger than base64I ran into this generating PDFs with pdf-lib, which returns a
Uint8Array. Same goes forfetch().arrayBuffer()or R2. The workaround from #543 (Buffer.from(...)) works on Node but needsBuffer, 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, usingBufferwhen it exists andbtoa(chunked) otherwise. Typed-array views respectbyteOffset/byteLength. Strings are passed through as before. SinceparseAttachmentsis shared, drafts and inbox thread replies/forwards get this too.Testing
btoafallback withBufferstubbed out, mixed attachments, a different PDF per email, and drafts. 9 of them fail oncanary.pnpm test,typecheck,lintandbuildall pass.canarytheUint8Arraysend 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
Buffercontent is now sent as base64 instead of{ type, data }. I tested that against the API and it works, but if you'd rather keepBufferas it was, I'm happy to limit the encoding toUint8Array/ArrayBuffer.