Skip to main content
The BosonMail SDK automatically switches to multipart/form-data when you include attachments. This guide covers sending a PDF invoice from disk, generating a CSV in memory, handling multiple files, validating sizes before sending, and securely accepting uploads from HTTP requests.

Attach a file from disk

Read a PDF with fs/promises and pass the resulting Buffer directly. Node.js Buffer is a Uint8Array subclass, so it satisfies the SDK type without conversion.
send-invoice.ts

Generate and attach a CSV in memory

Use TextEncoder to turn a CSV string into a Uint8Array, then attach it without writing to disk.
send-csv.ts

Validate sizes before sending

The SDK enforces limits at send time, but validating early lets you fail fast and give clear feedback to users. This helper mirrors the SDK rules so your application can pre-check files.
validate-attachments.ts

Handle HTTP file uploads server-side

If your application accepts file uploads from a browser or mobile app, read them on your server and pass the File or Blob objects to the SDK. Never expose your BosonMail API key in browser code.
handle-upload.ts

Security checklist

The SDK requires your BosonMail API key on every request. Keep it server-side. Upload files to your own backend, validate them, then call the SDK from there.
Check file types, sizes, and names before passing them to the SDK. The SDK validates content type and size too, but defense in depth protects your API quota and reduces unnecessary requests.
Do not log attachment content, recipient addresses, or the client object (it contains the API key). Log only message IDs and send outcomes for troubleshooting.
Ensure the endpoint that receives user uploads is served over HTTPS to prevent interception of files and metadata in transit.

Next steps

Multiple recipients

Send the same attachment to many recipients with To, CC, or BCC.

Monitoring delivery

Track whether an email with attachments was delivered or bounced.

SDK reference: Attachments

Read the concise SDK reference for attachment types and limits.

Production best practices

Learn how to secure keys, handle errors, and manage quotas in production.