fastify-xemail
send
Send a transactional email with HTML content via SendGrid.
send
Send a transactional email. Plain text content is auto-generated from the HTML body if not provided.
Signature
fastify.xEmail.send(
to: string | string[],
subject: string,
html: string,
text?: string | null,
extraOptions?: object
): Promise<{ success: boolean; statusCode: number; messageId: string }>
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. |
text | string | null | No | Plain text fallback. If omitted, HTML tags are stripped to generate plain text automatically. |
extraOptions | object | No | Additional SendGrid mail options merged into the message (e.g., replyTo, categories, headers). |
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 from the x-message-id response header. |
Throws
[xEmail] 'to' is required for send().— missing recipient.[xEmail] 'subject' is required for send().— missing subject.[xEmail] 'html' is required for send().— missing HTML body.[xEmail] Failed to send email: <reason>— SendGrid API error.
Examples
Basic — password reset email
fastify.post("/auth/forgot-password", async (request, reply) => {
const { email } = request.body;
const token = await generateResetToken(email);
const result = await fastify.xEmail.send(
email,
"Reset your password",
`<p>Click <a href="https://app.example.com/reset?token=${token}">here</a> to reset your password. This link expires in 1 hour.</p>`
);
return { sent: result.success };
});
Realistic — send to multiple recipients with extra options
fastify.post("/notifications/broadcast", async (request, reply) => {
const { emails, subject, body } = request.body;
const result = await fastify.xEmail.send(
emails,
subject,
`<div style="font-family: sans-serif;">${body}</div>`,
null,
{
categories: ["broadcast", "notification"],
headers: { "X-Campaign-ID": "summer-2024" },
}
);
return { statusCode: result.statusCode, messageId: result.messageId };
});
See Also
- send-template — Send using a SendGrid dynamic template instead of inline HTML
- send-with-attachments — Send with file attachments
- send-bulk — Send the same email to many recipients in one API call
AI Context
package: "@xenterprises/fastify-xemail"
method: fastify.xEmail.send(to, subject, html, options?)
use-when: Send a transactional email with inline HTML content to a single recipient
params: to (string), subject (string), html (string), options (from, cc, bcc, replyTo, text)
returns: Promise<void>
