Skip to content
DocumentationAPI referenceBuild something worth sending.

Guides

Send email with SvelteKit

Send email from a SvelteKit form action with the SendPanda SDK. The API key stays in the server environment.

Send from a SvelteKit form action in +page.server.ts or +server.ts. Those files run on the server, which is where the API key belongs. The SDK uses fetch.

1. Install

npm install @sendpanda/sdk

Create a Send emails key and store it as SENDPANDA_API_KEY. The from address has to be on a domain verified in that project. See domain setup. A queued status means the API accepted the email, not that the recipient's server delivered it.

2. Send from a form action

// src/routes/welcome/+page.server.ts
import { error } from "@sveltejs/kit";
import { SendPanda } from "@sendpanda/sdk";

const panda = new SendPanda(process.env.SENDPANDA_API_KEY!);

export const actions = {
  default: async ({ locals }) => {
    const user = locals.user;
    if (!user) error(401, "Sign in first");
    const email = await panda.emails.send(
      {
        from: "Your app <hello@your-verified-domain.com>",
        to: [user.email],
        subject: "Welcome",
        text: "Welcome",
      },
      { idempotencyKey: `welcome-${user.id}` },
    );
    return { id: email.id, status: email.status };
  },
};

locals.user is whatever your own hooks.server.ts sets. SvelteKit does not provide it. Read the address from that session, not from the form.

A route that sends to any address a request supplies is an open relay. Decide the recipient from your own session or database. Use one idempotency key per business event, and reuse that same key if the request is retried.

↑↓ to navigate↵ to openesc to close