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 withfs/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
UseTextEncoder 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 theFile or Blob objects to the SDK. Never expose your BosonMail API key in browser code.
handle-upload.ts
Security checklist
Never expose API keys in the browser
Never expose API keys in the browser
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.
Validate uploads in your app
Validate uploads in your app
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.
Avoid logging sensitive content
Avoid logging sensitive content
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.
Use HTTPS for uploads
Use HTTPS for uploads
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.