Send Emails

Configure email delivery and send transactional emails from your backend functions.

Send Emails

Set up email delivery and send transactional emails — password resets, welcome messages, order confirmations — from any backend function.

What you will build

- Email addon configured with SMTP or SendGrid
- POST /api/auth/forgot-password — sends a password reset email
- A reusable email template pattern

Steps

Install the email addon

Go to Addons and install the Email addon. Then add your provider's credentials in Settings > Environment Variables:

For SMTP:
    - SMTP_HOST — e.g., smtp.gmail.com
    - SMTP_PORT — typically 587
    - SMTP_USER — your email address
    - SMTP_PASS — your password or app password
For SendGrid:
    - SENDGRID_API_KEY — your API key from the SendGrid dashboard

Build the forgot-password endpoint

Create POST /api/auth/forgot-password:

1. Find One — Table: users, Where: { email: input.email }, Assign To: user
    2. If/Else — Condition: vars.user == null
       - True: Respond — Status: 200, Body: { message: 'If an account exists, a reset email has been sent' }
    3. Random String — Length: 32, Characters: hex, Assign To: resetToken
    4. Database Update — Table: users, Data: { reset_token: vars.resetToken, reset_expires: '' | now | addHours(1) | toISO }, Where: { id: vars.user.id }
    5. Send Email — To: vars.user.email, Subject: 'Password Reset Request', Body: your HTML template (see next step)
    6. Respond — Status: 200, Body: { message: 'If an account exists, a reset email has been sent' }

Create the email template

Use HTML in the Send Email body for formatted emails:

'<h1>Reset Your Password</h1>' +
    '<p>Hi ' + vars.user.name + ',</p>' +
    '<p>Click the link below to reset your password. This link expires in 1 hour.</p>' +
    '<a href="' + env.APP_URL + '/reset-password?token=' + vars.resetToken + '">Reset Password</a>'

Test email delivery

Use the Playground to call your forgot-password endpoint. During development, use a service like Mailtrap to capture test emails without sending real ones.

Pro tip

For production, use a dedicated email service like SendGrid, Mailgun, or Amazon SES. They offer better deliverability and higher sending limits than personal SMTP.

Add User Authentication
/guides/user-auth
Set up the user system that needs emails
Addons
/addons
Browse email and communication addons