what

Email

Send transactional emails via SMTP or the Resend API. Use HTML templates with #variable# syntax.

Email Configuration

Add an [email] section to what.toml. Choose either SMTP or Resend as the transport.

what.toml
[email]
from = "[email protected]"
from_name = "My App"
template_dir = "emails"     # default: "emails"
KeyDefaultDescription
from-Sender email address (required)
from_name-Display name (optional, e.g., "My App")
template_diremailsDirectory containing email templates

SMTP Setup

Configure SMTP credentials under [email.smtp]. Use ${ENV_VAR} syntax to keep secrets out of your config file.

what.toml
[email]
from = "[email protected]"
from_name = "My App"

[email.smtp]
host = "smtp.mailgun.org"
port = 587                     # default: 587
username = "${SMTP_USER}"
password = "${SMTP_PASS}"

The ${SMTP_USER} and ${SMTP_PASS} placeholders are resolved from environment variables at send time. If the variable is not set, the raw string is used as-is (and a warning is logged).

Tip: Most SMTP providers use port 587 with STARTTLS. The framework connects using STARTTLS by default.

Resend API

Use the Resend API as an alternative to SMTP:

what.toml
[email]
from = "[email protected]"

[email.api]
provider = "resend"
api_key = "${RESEND_API_KEY}"

Configure either [email.smtp] or [email.api], not both. The framework uses whichever is present.

Email Templates

Email templates are HTML files in the emails/ directory (or whatever template_dir is set to). They use the same #variable# syntax as page templates.

emails/welcome.html
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif; padding: 20px;">
  <h1>Welcome, #name#!</h1>
  <p>Thanks for signing up. Your account is ready.</p>
  <p>
    <a href="https://example.com/dashboard"
       style="background: #2563eb; color: white; padding: 10px 20px;
              text-decoration: none; border-radius: 4px;">
      Go to Dashboard
    </a>
  </p>
  <p style="color: #666; font-size: 0.875rem;">
    If you didn't create this account, you can ignore this email.
  </p>
</body>
</html>

Templates support all filters: #name|capitalize#, #amount|currency#, #date|date:"%B %d, %Y"#, etc.

Sending from Forms

Configure each email in what.toml; a form names it with a w-email hidden field and chooses nothing else:

what.toml
[collections.contacts]
create = "all"

[email.actions.new-contact]
collection = "contacts"          # only a write to this collection sends it
to = "[email protected]"          # one address
subject = "New contact form submission"
template = "contact-notification"             # emails/contact-notification.html: a plain name, no path
on = ["create"]                  # create, update and/or delete (default ["create"])
site/contact.html
<form action="/w-action/contacts" method="POST">
  <input type="hidden" name="w-email" value="new-contact">

  <label>Name</label>
  <input type="text" name="name" required>

  <label>Email</label>
  <input type="email" name="email" required>

  <label>Message</label>
  <textarea name="message" required></textarea>

  <button type="submit" class="btn btn-primary">Send</button>
</form>
Hidden FieldDescription
w-emailThe name of an [email.actions.<name>] in what.toml: it decides the recipient, subject and template

The email is queued once, after the record is saved. A refused or stale write sends nothing, and so does a form naming no action, or an action for another collection or kind of write (the log has a what::email warning). An update or delete that finds no record to change sends nothing. Delivery is best effort: if the job queue is full or closed, the record stays saved and the email is dropped with a what::email warning. The 2.0.0 fields w-email-to, w-email-subject and w-email-template are ignored.

The template is rendered without the form's values: write it as the notice you want to receive, and read the new record on your site. (The 2.0.0 documentation said form values were available in it; they never were.)

emails/contact-notification.html
<h2>New contact submission</h2>
<p>Someone sent the contact form. Open the contacts page to read it.</p>

Async Delivery

Emails are sent via a background job queue. The form submission responds immediately while the email is delivered asynchronously. This means:

  • Users don't wait for the SMTP handshake or API call
  • A slow or failed email delivery doesn't affect the form response
  • Failed deliveries are logged but don't return errors to the user
Note: An [email.actions.<name>] needs an [email] section with a transport that can send ([email.smtp], or [email.api] with provider = "resend"), a declared collection and, if it names one, a template file inside the template directory; otherwise the site does not start. A form whose email is not sent still saves its record.