Mailgun allows for passing dynamic data into your templates using either Template Variables or Recipient Variables. Template variables are shared data (think company or promo information, etc) that remain the same in the delivered email regardless of the recipient, while recipient variables (think name, shipping address, items purchased, etc) are keyed to the recipient.

# Using recipient variables in Mailgun templates

## Overview

You can pass `recipient-variables` as nested JSON, and use those values directly in Mailgun templates with Handlebars. Each recipient's variables are available in the template under `recipient.<key>`, so one template and one API call can produce a different, fully personalized message for every recipient.

This guide is for developers who send templated email with the Messages API and want per-recipient data such as customer details, order items, or plan tier.

## How it works

Pass `recipient-variables` as a JSON object keyed by recipient email address. Each value can be a nested object, and can contain arrays. In the template, reference a value with its path under `recipient`.

```json
{
  "chris@example.com": {
    "customer": { "firstName": "Chris" },
    "order": {
      "number": "CX-100245",
      "items": [
        { "name": "Wireless Headphones", "quantity": 1 }
      ]
    }
  }
}
```

```handlebars
Hi {{recipient.customer.firstName}},
Order #{{recipient.order.number}}
{{#each recipient.order.items}}
  {{this.name}} x {{this.quantity}}
{{/each}}
```

- The key must match the address in `to` exactly, including case.
- Nested objects and arrays work with the Handlebars helpers Mailgun supports: `if`, `unless`, `each`, `with`, and `equal`.
- Values passed in `t:variables` stay at the top level of the template and don't use the `recipient.` prefix.


## Quick start

Store a template that reads from `recipient`, then send with `recipient-variables`.

**1. Create the template**

```bash
curl -s --user 'api:YOUR_API_KEY' \
    https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates \
    -F name='order-confirmation' \
    -F description='Order confirmation' \
    --form-string template='<h1>Thanks, {{recipient.customer.firstName}}!</h1>
<p>Order #{{recipient.order.number}}</p>
<ul>
{{#each recipient.order.items}}
  <li>{{this.name}} x {{this.quantity}}</li>
{{/each}}
</ul>'
```

**2. Send a message**

```bash
curl -s --user 'api:YOUR_API_KEY' \
    https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages \
    -F from='Orders <orders@YOUR_DOMAIN_NAME>' \
    -F to='chris@example.com' \
    -F subject='Your order is confirmed' \
    -F template='order-confirmation' \
    -F recipient-variables='{
      "chris@example.com": {
        "customer": { "firstName": "Chris" },
        "order": {
          "number": "CX-100245",
          "items": [
            { "name": "Wireless Headphones", "quantity": 1 },
            { "name": "Phone Case", "quantity": 2 }
          ]
        }
      }
    }'
```

## Using nested values with Handlebars helpers

Mailgun supports five block helpers. Each one works with nested `recipient` values.

| Helper | Use it to | Example |
|  --- | --- | --- |
| `if` / `else if` / `else` | Show content when a value is true | `{{#if recipient.order.paymentReceived}}` |
| `unless` | Show content when a value is false or missing | `{{#unless recipient.shipping.trackingNumber}}` |
| `each` | Loop over an array | `{{#each recipient.order.items}}` |
| `with` | Move into a nested object so you can skip the repeated path | `{{#with recipient.customer}}` |
| `equal` | Match a value exactly | `{{#equal recipient.membership.tier "plus"}}` |


**`with`: shorten repeated paths**

```handlebars
{{#with recipient.shipping.address}}
  {{street}}, {{city}}, {{state}} {{zip}}
{{/with}}
```

**`each` with `equal`: flag individual items**

```handlebars
{{#each recipient.order.items}}
  {{this.name}}
  {{#equal this.backordered "yes"}}(backordered){{/equal}}
{{/each}}
```

**`if` / `else if` / `else`: choose a language**

```handlebars
{{#if recipient.spanish}}
  ¡Gracias por tu compra!
{{else if recipient.french}}
  Merci pour votre achat !
{{else}}
  Thanks for your order!
{{/if}}
```

`equal` converts both sides to strings before comparing, so `5` and `"5"` match.

## Sending to multiple recipients

Add one key per recipient. List every address in `to` (comma-separated), and each recipient receives their own message rendered from their own values. Recipients can't see each other.

```bash
-F to='chris@example.com,maria@example.com' \
-F recipient-variables='{
  "chris@example.com": {
    "spanish": false,
    "order": { "number": "CX-100245", "paymentReceived": true }
  },
  "maria@example.com": {
    "spanish": true,
    "order": { "number": "CX-100311", "paymentReceived": false }
  }
}'
```

With the template from the previous section, Chris gets an English message with payment confirmed, and Maria gets a Spanish greeting with a payment-pending notice.

The subject and sender are shared by everyone in the batch. A batch can contain up to 1,000 recipients.

## Combining with shared values

Use `recipient-variables` for values that differ per recipient, and `t:variables` for values that are the same for everyone in the send, such as company details or a promotion. Shared values are referenced at the top level, with no `recipient.` prefix.

```handlebars
{{#if promo}}
  {{promo.headline}} Use code {{promo.code}}.
{{/if}}

Hi {{recipient.customer.firstName}},

Questions? Email {{company.supportEmail}}.
```

```bash
-F t:variables='{
  "company": { "supportEmail": "support@example.com" },
  "promo": { "headline": "Free shipping on your next order.", "code": "THANKS10" }
}' \
-F recipient-variables='{ "chris@example.com": { "customer": { "firstName": "Chris" } } }'
```

Because the two sources live in separate places in the template, a shared name and a recipient name can't collide.

## Limits and notes

- A batch supports up to 1,000 recipients.
- Send options (parameters starting with `o:`, `h:`, `v:`, or `t:`) are limited to 16KB combined.
- The `equal` helper compares as strings, so `5` and `"5"` are equal.
- Templates are region-specific. Create the template in each region you send from.
- Each account, and each sending domain for domain-level templates, can store up to 100 templates with 40 versions per template.
- Updating a template: add a new version and make it active, or send a specific version with `t:version`.


## Troubleshooting

| Symptom | Likely cause | Fix |
|  --- | --- | --- |
| Fields render blank | The template reads `{{customer.firstName}}` instead of `{{recipient.customer.firstName}}` | Prefix recipient values with `recipient.` |
| One recipient's fields are blank | The JSON key doesn't match the address in `to` | Use the exact address, including case |
| Changes to the template don't appear | The send is still using the previous active version | Make the new version active, or pass `t:version` |
| A recipient never gets the message | The address is invalid, suppressed, or not authorized on a sandbox domain | Check Logs for that address |
| Request returns a 400 error | `recipient-variables` isn't valid JSON | Validate the JSON before sending |
| Shared values are blank | They were sent in `recipient-variables` but referenced without `recipient.`, or the reverse | Match each reference to where the value was sent |