fastify-xemail
sendWithAttachments
Send an email with one or more file attachments via SendGrid.
sendWithAttachments
Send an email with one or more base64-encoded file attachments.
Signature
fastify.xEmail.sendWithAttachments(
to: string | string[],
subject: string,
html: string,
attachments: Attachment[]
): Promise<{ success: boolean; statusCode: number; messageId: string }>
interface Attachment {
content: string; // base64-encoded file content
filename: string; // file name shown to the recipient
type: string; // MIME type (e.g., "application/pdf")
disposition?: string; // "attachment" (default) or "inline"
}
Params
| Name | Type | Required | Description |
|---|---|---|---|
to | string | string[] | Yes | Recipient email address or array of addresses. |
subject | string | Yes | Email subject line. |
html | string | Yes | HTML email body. |
attachments | Attachment[] | Yes | Non-empty array of attachment objects. |
Returns
Promise<{ success: boolean; statusCode: number; messageId: string }>
| Property | Type | Description |
|---|---|---|
success | boolean | Always true on resolved promise. |
statusCode | number | SendGrid HTTP response status (typically 202). |
messageId | string | SendGrid message ID. |
Throws
[xEmail] 'to' is required for sendWithAttachments().[xEmail] 'subject' is required for sendWithAttachments().[xEmail] 'html' is required for sendWithAttachments().[xEmail] 'attachments' must be a non-empty array.[xEmail] Failed to send email with attachments: <reason>— SendGrid API error.
Examples
Basic — send a PDF invoice
import { readFileSync } from "fs";
fastify.post("/invoices/:id/email", async (request) => {
const invoice = await db.invoices.findById(request.params.id);
const pdfBuffer = await generateInvoicePdf(invoice);
return fastify.xEmail.sendWithAttachments(
invoice.customerEmail,
`Invoice #${invoice.number}`,
`<p>Please find your invoice #${invoice.number} attached.</p>`,
[{
content: pdfBuffer.toString("base64"),
filename: `invoice-${invoice.number}.pdf`,
type: "application/pdf",
}]
);
});
Realistic — multiple attachments (PDF + CSV)
fastify.post("/reports/:id/email", async (request) => {
const report = await generateReport(request.params.id);
return fastify.xEmail.sendWithAttachments(
request.body.recipientEmail,
`Report: ${report.title}`,
`<p>Your report <strong>${report.title}</strong> is attached in two formats.</p>`,
[
{
content: report.pdf.toString("base64"),
filename: `${report.slug}.pdf`,
type: "application/pdf",
},
{
content: Buffer.from(report.csv).toString("base64"),
filename: `${report.slug}.csv`,
type: "text/csv",
},
]
);
});
See Also
- send — Send without attachments
- send-template — Send using a SendGrid dynamic template
AI Context
package: "@xenterprises/fastify-xemail"
method: fastify.xEmail.sendWithAttachments(to, subject, html, attachments, options?)
use-when: Send a transactional email with one or more file attachments (base64-encoded buffers or strings)
params: to, subject, html, attachments (array of {content, filename, type, disposition}), options
returns: Promise<void>
