Quick setup examples

RedwoodJS

Send your first email from a RedwoodJS serverless function using the mailblastr SDK.

Send your first email from a RedwoodJS app using the mailblastr Node SDK. Install it with npm install mailblastr, then import MailBlastr and call mb.emails.send(). Redwood serverless functions live in the api side, so the call (and your key) stay on the server.

Prerequisites

1. Install the SDK

yarn workspace api add mailblastr

2. Add your API key

Add your key to the project .env. Redwood loads it into the api side automatically.

.env
MAILBLASTR_API_KEY=mb_xxxxxxxxx

3. Generate a function

Scaffold a serverless function on the api side.

yarn rw g function send

4. Send email from the function

Edit the generated handler to use the SDK.

api/src/functions/send/send.ts
import type { APIGatewayEvent, Context } from 'aws-lambda';
import { MailBlastr } from 'mailblastr';

const mb = new MailBlastr(process.env.MAILBLASTR_API_KEY);

export const handler = async (_event: APIGatewayEvent, _context: Context) => {
  const { data, error } = await mb.emails.send({
    from: 'Acme <onboarding@yourdomain.com>',
    to: ['delivered@example.com'],
    subject: 'hello world',
    html: '<strong>it works!</strong>',
  });

  return {
    statusCode: error ? 500 : 200,
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(error ? { error } : { data }),
  };
};
A successful call returns the created email's id. See the full request body in the Send an email reference.