# Templates

> Templates with variables, versions, languages and a preview, kept with your project.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://mail.docs.sypnia.com/sending/templates/

For agents: Mail templates: name, subject, HTML, optional text (generated from HTML); {{var}} Handlebars-style (#if, #each, escaping); preview with data; versions: sends use published, templates publish/rollback --to N, pin template@3; per-language versions chosen by locale with default fallback; templates pull/push to mail/templates/<name>/ (subject.txt, body.html, body.txt, meta.json); templates as React components (@sypnia/mail/react, rendered by templates push); Data sign-in e-mails: Data provider sypnia_mail with a from on a verified domain, templates stay in Data, Mail sends, logs with tag kind=auth.<type>, suppresses bounces, counted with the organization's mail (10 free a month, then credits).

A template has a name, a subject, an HTML body and an optional text body. Variables use `{{name}}`; `{{#if}}`, `{{#each}}` and escaping work as in Handlebars. If you leave out the text body, it is made from the HTML.

```bash
npx @sypnia/mail templates create order-ready --subject "Tu pedido {{order}} está listo" --html ./order-ready.html
npx @sypnia/mail templates preview order-ready --data '{"name":"Ana","order":"#1042"}'
```

## Versions

Every change makes a new version; sends use the **published** one. `templates publish order-ready` publishes the latest draft, `templates rollback order-ready --to 3` goes back. A send can pin a version with `template: 'order-ready@3'`.

## Languages

A template can have a version per language (`es`, `en`, `pt-BR`). The send picks it with `locale`; without a match it uses the template's default language.

## Templates as files

`mail templates pull` writes every template to `mail/templates/<name>/` (`subject.txt`, `body.html`, `body.txt`, `meta.json`) and `mail templates push` sends them back, so templates can live in your repository and be reviewed like code.

## Templates as React components

If you prefer writing e-mails as components, `@sypnia/mail/react` renders them to HTML that mail apps show correctly:

`mail/order-ready.tsx`

```tsx
import { Email, Section, Button } from '@sypnia/mail/react';

export default function OrderReady({ name, order }: { name: string; order: string }) {
  return (
    <Email preview={`Your order ${order} is ready`}>
      <Section>Hi {name}, your order {order} is ready to pick up.</Section>
      <Button href="https://laespiga.co/orders">See your order</Button>
    </Email>
  );
}
```

`npx @sypnia/mail templates push` renders each component in `mail/` and stores the result as a new version of the template with the same name, so sends, versions, languages and previews work exactly as above.

## Sign-in e-mails of Sypnia Data

Sypnia Mail is one of the e-mail providers a [Sypnia Data](https://data.docs.sypnia.com/auth/email-providers-and-templates) project can choose for its sign-in e-mails: confirmations, magic links, codes, password recovery, invitations and e-mail changes. Choose `sypnia_mail` in the project's auth settings and give a `from` address on a domain verified here; nothing else is needed.

- **The words stay in Data.** Each type of sign-in e-mail keeps its template in Data, with the variables Data documents; Mail sends what Data writes, from your domain.
- **Everything else is Mail's.** Each e-mail is in the [event log](https://mail.docs.sypnia.com/events/event-log) with the tag `kind=auth.<type>`, bounces are suppressed, and the domain's limits and reputation apply as for any send.
- **Paid like any e-mail.** The sends are counted with the rest of your organization's mail: the first 10 of each month are free, and the rest are paid with your organization's credits.
