attachments field in SendEmailBody. When attachments are present, the request automatically switches from JSON to multipart/form-data transport. The SDK validates attachment count, filename presence, content type, and size limits client-side before sending.
SendAttachment interface
string
required
The name of the file as it appears to the recipient. Must be non-empty.
Blob | Uint8Array | ArrayBuffer
required
The binary content of the file. Node
Buffer is a Uint8Array subclass and is accepted.string
MIME type for the attachment. Defaults to
"application/octet-stream" when the SDK wraps Uint8Array or ArrayBuffer in a Blob. Ignored when content is already a Blob or File (the Blob’s own type is used instead).Supported content types
Size limits
The SDK enforces the following limits client-side. These match the BosonMail API limits:The SDK calculates sizes using binary mebibytes:
10 * 1024 * 1024 and 20 * 1024 * 1024 bytes.Transport: multipart/form-data
Whenattachments is present, the SDK sends a multipart/form-data request to POST /emails/send. The SDK does not set the Content-Type header; fetch generates the boundary automatically.
The form fields are:
Examples
Read a file from disk (Node.js)
attach-file.ts
Read a file with Bun
Bun’sBun.file() returns a Blob-compatible object. Pass arrayBuffer() to be safe:
attach-file-bun.ts
Generate text content programmatically
attach-generated.ts
Multiple attachments
attach-multiple.ts
Pre-validating size in your application
You can check limits before calling the SDK to provide friendlier error messages:validate-attachments.ts
Security considerations
- Validate uploads in your application. Check file types, sizes, and filenames before passing them to the SDK. Reject unexpected MIME types.
- Sanitize filenames. Strip path separators and control characters. Use a safe filename or generate one server-side.
- Restrict MIME types. Only allow attachment types your application expects (for example, PDF, CSV, images). Block executable files.
- Scan for malware. Run uploaded files through an antivirus or malware scanner before attaching them to outbound email.
- Do not attach secrets. Never attach API keys, credentials, or sensitive documents to automated emails.
- Keep within limits. Large attachments increase timeout risk and may be rejected by recipient servers. Consider hosting large files and linking instead.
- Server-side only. Call the SDK from your backend, never from browser code. The API key must not be exposed to clients.
Sending emails
Full sendEmail reference with parameters and validation rules.
CC and BCC
How CC and BCC work with attachment requests.
Guides: Attachments
Step-by-step guide for handling file uploads and attaching them to emails.
Error handling
How to handle attachment validation and API errors.