# Send with a template

> Reference a saved template from POST /emails and fill its variables.

To send an email from a template, pass `template_id` (and any `variables`) to `POST /emails` instead of inline `html`/`text`. MailBlastr loads the template, substitutes the variables, and sends the result. Everything else about the send — `from`, `to`, attachments, scheduling, tracking — works exactly as a normal send.

**Node.js**

```js
import { MailBlastr } from 'mailblastr';

const mb = new MailBlastr('mb_xxxxxxxxx');

const { data, error } = await mb.emails.send({
  "from": "Acme <hello@yourdomain.com>",
  "to": ["user@example.com"],
  "template_id": "tmpl_8f5c2a1e",
  "variables": { "first_name": "Ada", "company": "Acme" }
});
console.log({ data, error });
```

**Ruby**

```ruby
require "mailblastr"

Mailblastr.api_key = "mb_xxxxxxxxx"

Mailblastr::Emails.send({
  "from": "Acme <hello@yourdomain.com>",
  "to": [
    "user@example.com"
  ],
  "template_id": "tmpl_8f5c2a1e",
  "variables": {
    "first_name": "Ada",
    "company": "Acme"
  }
})
```

**PHP**

```php
$mailblastr = Mailblastr::client('mb_xxxxxxxxx');

$mailblastr->emails->send([
  'from' => "Acme <hello@yourdomain.com>",
  'to' => [
    "user@example.com"
  ],
  'template_id' => "tmpl_8f5c2a1e",
  'variables' => [
    'first_name' => "Ada",
    'company' => "Acme"
  ]
]);
```

**Python**

```python
import mailblastr

mailblastr.api_key = "mb_xxxxxxxxx"

mailblastr.Emails.send({
  "from": "Acme <hello@yourdomain.com>",
  "to": [
    "user@example.com"
  ],
  "template_id": "tmpl_8f5c2a1e",
  "variables": {
    "first_name": "Ada",
    "company": "Acme"
  }
})
```

**Go**

```go
import "github.com/shekhu10/mailblastr-sdks/mailblastr-go"

client := mailblastr.NewClient("mb_xxxxxxxxx")

params := &mailblastr.SendEmailRequest{
    From:    "Acme <hello@yourdomain.com>",
    To:      []string{"delivered@example.com"},
    Subject: "hello world",
    Html:    "<p>it works!</p>",
}
sent, err := client.Emails.Send(params)
```

**Rust**

```rust
use mailblastr::{Mailblastr, CreateEmailBaseOptions};

let mb = Mailblastr::new("mb_xxxxxxxxx");

let email = CreateEmailBaseOptions::new(
    "Acme <hello@yourdomain.com>",
    ["delivered@example.com"],
    "hello world",
).with_html("<p>it works!</p>");
let _sent = mb.emails.send(email).await?;
```

**Java**

```java
import com.mailblastr.*;

Mailblastr mailblastr = new Mailblastr("mb_xxxxxxxxx");

SendEmailRequest request = SendEmailRequest.builder()
    .from("Acme <hello@yourdomain.com>")
    .to("delivered@example.com")
    .subject("hello world")
    .html("<p>it works!</p>")
    .build();
mailblastr.emails().send(request);
```

**.NET**

```csharp
using Mailblastr;

IMailblastr mailblastr = MailblastrClient.Create("mb_xxxxxxxxx");

var resp = await mailblastr.EmailSendAsync(new EmailMessage
{
    From = "Acme <hello@yourdomain.com>",
    To = "delivered@example.com",
    Subject = "hello world",
    HtmlBody = "<p>it works!</p>",
});
```

**cURL**

```bash
curl -X POST 'https://api.mailblastr.com/emails' \
  -H 'Authorization: Bearer mb_xxxxxxxxx' \
  -H 'Content-Type: application/json' \
  -d '{
  "from": "Acme <hello@yourdomain.com>",
  "to": ["user@example.com"],
  "template_id": "tmpl_8f5c2a1e",
  "variables": { "first_name": "Ada", "company": "Acme" }
}'
```

**CLI**

```bash
mailblastr emails send \
  --from 'Acme <hello@yourdomain.com>' \
  --to 'user@example.com' \
  --template-id 'tmpl_8f5c2a1e' \
  --variables '{"first_name":"Ada","company":"Acme"}'
```

## Overrides

Any field you set directly on the request wins over the template. For example, passing `subject` alongside `template_id` uses your subject and the template's body. This lets one template back many variations. The `from`, `subject`, and `reply_to` in the payload take precedence over the template's defaults for these fields; if the template defines no default for one of them, you must supply it in the payload.

> **Warning:** When a `template_id` is provided, you cannot also send `html` or `text` in the same request — doing so returns a `validation_error`.

## Variables and fallbacks

Each value in `variables` replaces the matching `{{ key }}` placeholder. If you omit a variable that the template defines, its `fallback_value` is used. If that variable has no fallback value, the email is **not** sent and a `validation_error` is returned.

> **Warning:** If the resolved email has neither `html` nor `text` (e.g. the template has only a subject and you sent no body), the request fails with `validation_error`. A `template_id` that isn't yours returns `not_found`.
