Quick setup examples
Astro
Send your first email from an Astro Action using the mailblastr SDK.
Send your first email from an Astro app using the mailblastr Node SDK. Install it with npm install mailblastr, then import MailBlastr and call mb.emails.send(). Because Astro runs server code on demand, an Astro Action is the natural place to make the call.
Prerequisites
- A MailBlastr API key.
- A verified domain to send from.
- An SSR adapter installed so routes can run on demand.
1. Install the SDK
npm install mailblastr2. Add your API key
Add your key to a .env file at the project root. Astro exposes it server-side via import.meta.env.
.env
MAILBLASTR_API_KEY=mb_xxxxxxxxx3. Send email from an Action
Create an Astro Action under src/actions/index.ts. Instantiate the client with the key from import.meta.env, call mb.emails.send(), and surface any error as an ActionError.
src/actions/index.ts
import { ActionError, defineAction } from 'astro:actions';
import { MailBlastr } from 'mailblastr';
const mb = new MailBlastr(import.meta.env.MAILBLASTR_API_KEY);
export const server = {
send: defineAction({
accept: 'form',
handler: async () => {
const { data, error } = await mb.emails.send({
from: 'Acme <onboarding@yourdomain.com>',
to: ['delivered@example.com'],
subject: 'Hello world',
html: '<strong>It works!</strong>',
});
if (error) {
throw new ActionError({ code: 'BAD_REQUEST', message: JSON.stringify(error) });
}
return data;
},
}),
};Call the send action from any frontmatter route, script, or component.
A successful call returns the created email's
id. See every supported field — cc, bcc, reply_to, attachments, scheduled_at — in the Send an email reference.