X Enterprises
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

NameTypeRequiredDescription
tostring | string[]YesRecipient email address or array of addresses.
subjectstringYesEmail subject line.
htmlstringYesHTML email body.
textstring | nullNoPlain text fallback. If omitted, HTML tags are stripped to generate plain text automatically.
extraOptionsobjectNoAdditional SendGrid mail options merged into the message (e.g., replyTo, categories, headers).

Returns

Promise<{ success: boolean; statusCode: number; messageId: string }>

PropertyTypeDescription
successbooleanAlways true on resolved promise.
statusCodenumberSendGrid HTTP response status (typically 202).
messageIdstringSendGrid 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

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>
Copyright © 2026