Skip to main content

Email Variables

Personalize your emails using dynamic variables and templates.

Basic Variables

Use double curly braces for variable substitution:
await lettr.emails.send({
  from: 'you@example.com',
  to: ['recipient@example.com'],
  subject: 'Welcome, {{name}}!',
  html: '<p>Hello {{name}}, thanks for joining {{company}}!</p>',
  variables: {
    name: 'John',
    company: 'Acme Inc'
  }
});

Per-Recipient Variables

Send personalized emails to multiple recipients:
await lettr.emails.send({
  from: 'you@example.com',
  to: ['john@example.com', 'jane@example.com'],
  subject: 'Your order #{{order_id}}',
  html: '<p>Hi {{name}}, your order is confirmed!</p>',
  recipientVariables: {
    'john@example.com': { name: 'John', order_id: '12345' },
    'jane@example.com': { name: 'Jane', order_id: '12346' }
  }
});

Default Values

Provide fallback values for missing variables:
// Using default syntax
html: '<p>Hello {{name|default:"Friend"}}!</p>'

Conditional Content

Show or hide content based on variables:
{{#if premium}}
  <p>Thank you for being a premium member!</p>
{{else}}
  <p>Upgrade to premium for exclusive benefits.</p>
{{/if}}

Loops

Iterate over arrays:
await lettr.emails.send({
  from: 'you@example.com',
  to: ['recipient@example.com'],
  subject: 'Your Order Summary',
  html: `
    <h2>Order Items</h2>
    <ul>
      {{#each items}}
        <li>{{this.name}} - ${{this.price}}</li>
      {{/each}}
    </ul>
    <p>Total: ${{total}}</p>
  `,
  variables: {
    items: [
      { name: 'Widget', price: '9.99' },
      { name: 'Gadget', price: '19.99' }
    ],
    total: '29.98'
  }
});

Built-in Variables

These variables are automatically available:
VariableDescription
{{unsubscribe_url}}Unsubscribe link
{{preferences_url}}Email preferences link
{{view_in_browser_url}}View in browser link
{{current_year}}Current year
{{recipient_email}}Recipient’s email address

Formatting Helpers

<!-- Date formatting -->
<p>Order date: {{formatDate order_date "MMMM D, YYYY"}}</p>

<!-- Number formatting -->
<p>Total: {{formatCurrency amount "USD"}}</p>

<!-- Uppercase -->
<p>Hello {{uppercase name}}!</p>