# Send Email

> Send a templated email to the contact as a step in your automation.

The **send email** step sends a [template](https://www.mailblastr.com/docs/templates/overview) email to the contact running through the automation. After adding a [trigger](https://www.mailblastr.com/docs/automations/trigger), add this step, select a published template, and configure the subject and sender address. Over the API, add a `send_email` step to the `steps` array. See [Using automations](https://www.mailblastr.com/docs/automations/overview) for the wider flow.

## How it works

**Node.js**

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

const mb = new MailBlastr('mb_xxxxxxxxx');

const { data, error } = await mb.automations.create({
  "name": "Welcome series",
  "domain": "yourdomain.com",
  "steps": [
    {
      "key": "start",
      "type": "trigger",
      "config": { "event_name": "user.created" }
    },
    {
      "key": "welcome",
      "type": "send_email",
      "config": {
        "template": { "id": "044db673-fff6-420f-a566-f6aba05d60e7" }
      }
    }
  ],
  "connections": [
    { "from": "start", "to": "welcome" }
  ]
});
console.log({ data, error });
```

**Ruby**

```ruby
require "mailblastr"

Mailblastr.api_key = "mb_xxxxxxxxx"

Mailblastr::Automations.create({
  "name": "Welcome series",
  "domain": "yourdomain.com",
  "steps": [
    {
      "key": "start",
      "type": "trigger",
      "config": {
        "event_name": "user.created"
      }
    },
    {
      "key": "welcome",
      "type": "send_email",
      "config": {
        "template": {
          "id": "044db673-fff6-420f-a566-f6aba05d60e7"
        }
      }
    }
  ],
  "connections": [
    {
      "from": "start",
      "to": "welcome"
    }
  ]
})
```

**PHP**

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

$mailblastr->automations->create([
  'name' => "Welcome series",
  'domain' => "yourdomain.com",
  'steps' => [
    [
      'key' => "start",
      'type' => "trigger",
      'config' => [
        'event_name' => "user.created"
      ]
    ],
    [
      'key' => "welcome",
      'type' => "send_email",
      'config' => [
        'template' => [
          'id' => "044db673-fff6-420f-a566-f6aba05d60e7"
        ]
      ]
    ]
  ],
  'connections' => [
    [
      'from' => "start",
      'to' => "welcome"
    ]
  ]
]);
```

**Python**

```python
import mailblastr

mailblastr.api_key = "mb_xxxxxxxxx"

mailblastr.Automations.create({
  "name": "Welcome series",
  "domain": "yourdomain.com",
  "steps": [
    {
      "key": "start",
      "type": "trigger",
      "config": {
        "event_name": "user.created"
      }
    },
    {
      "key": "welcome",
      "type": "send_email",
      "config": {
        "template": {
          "id": "044db673-fff6-420f-a566-f6aba05d60e7"
        }
      }
    }
  ],
  "connections": [
    {
      "from": "start",
      "to": "welcome"
    }
  ]
})
```

**Go**

```go
package main

import (
    "fmt"
    "io"
    "net/http"
    "strings"
)

func main() {
    req, _ := http.NewRequest("POST", "https://api.mailblastr.com/automations", strings.NewReader(`{
  "name": "Welcome series",
  "domain": "yourdomain.com",
  "steps": [
    {
      "key": "start",
      "type": "trigger",
      "config": { "event_name": "user.created" }
    },
    {
      "key": "welcome",
      "type": "send_email",
      "config": {
        "template": { "id": "044db673-fff6-420f-a566-f6aba05d60e7" }
      }
    }
  ],
  "connections": [
    { "from": "start", "to": "welcome" }
  ]
}`))
    req.Header.Set("Authorization", "Bearer mb_xxxxxxxxx")
    req.Header.Set("Content-Type", "application/json")
    res, _ := http.DefaultClient.Do(req)
    defer res.Body.Close()
    out, _ := io.ReadAll(res.Body)
    fmt.Println(string(out))
}
```

**Rust**

```rust
use reqwest::Client;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let res = Client::new()
        .post("https://api.mailblastr.com/automations")
        .header("Authorization", "Bearer mb_xxxxxxxxx")
        .header("Content-Type", "application/json")
        .body(r#"{
  "name": "Welcome series",
  "domain": "yourdomain.com",
  "steps": [
    {
      "key": "start",
      "type": "trigger",
      "config": { "event_name": "user.created" }
    },
    {
      "key": "welcome",
      "type": "send_email",
      "config": {
        "template": { "id": "044db673-fff6-420f-a566-f6aba05d60e7" }
      }
    }
  ],
  "connections": [
    { "from": "start", "to": "welcome" }
  ]
}"#)
        .send()
        .await?;
    println!("{}", res.text().await?);
    Ok(())
}
```

**Java**

```java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

var client = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder()
    .uri(URI.create("https://api.mailblastr.com/automations"))
    .header("Authorization", "Bearer mb_xxxxxxxxx")
    .header("Content-Type", "application/json")
    .method("POST", HttpRequest.BodyPublishers.ofString("""
{
  "name": "Welcome series",
  "domain": "yourdomain.com",
  "steps": [
    {
      "key": "start",
      "type": "trigger",
      "config": { "event_name": "user.created" }
    },
    {
      "key": "welcome",
      "type": "send_email",
      "config": {
        "template": { "id": "044db673-fff6-420f-a566-f6aba05d60e7" }
      }
    }
  ],
  "connections": [
    { "from": "start", "to": "welcome" }
  ]
}
"""))
    .build();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
```

**.NET**

```csharp
using System.Net.Http;
using System.Text;

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.mailblastr.com/automations");
request.Headers.Add("Authorization", "Bearer mb_xxxxxxxxx");
request.Content = new StringContent(@"{
  ""name"": ""Welcome series"",
  ""domain"": ""yourdomain.com"",
  ""steps"": [
    {
      ""key"": ""start"",
      ""type"": ""trigger"",
      ""config"": { ""event_name"": ""user.created"" }
    },
    {
      ""key"": ""welcome"",
      ""type"": ""send_email"",
      ""config"": {
        ""template"": { ""id"": ""044db673-fff6-420f-a566-f6aba05d60e7"" }
      }
    }
  ],
  ""connections"": [
    { ""from"": ""start"", ""to"": ""welcome"" }
  ]
}", Encoding.UTF8, "application/json");
var response = await client.SendAsync(request);
Console.WriteLine(await response.Content.ReadAsStringAsync());
```

**cURL**

```bash
curl -X POST 'https://api.mailblastr.com/automations' \
  -H 'Authorization: Bearer mb_xxxxxxxxx' \
  -H 'Content-Type: application/json' \
  -d '{
  "name": "Welcome series",
  "domain": "yourdomain.com",
  "steps": [
    {
      "key": "start",
      "type": "trigger",
      "config": { "event_name": "user.created" }
    },
    {
      "key": "welcome",
      "type": "send_email",
      "config": {
        "template": { "id": "044db673-fff6-420f-a566-f6aba05d60e7" }
      }
    }
  ],
  "connections": [
    { "from": "start", "to": "welcome" }
  ]
}'
```

> **Note:** Only **published** templates can be used in an automation.

## Template variables

Use the `variables` field to pass data into your template. Each value can be a dynamic reference or a static string.

| Type | Format | Description |
| --- | --- | --- |
| Event data | `{ "var": "event.<field>" }` | Resolves a field from the triggering event's payload. |
| Contact data | `{ "var": "contact.<field>" }` | Resolves a field from the contact record. |
| Waited event data | `{ "var": "wait_events.<event>.<field>" }` | Resolves a field from a preceding [wait for event](https://www.mailblastr.com/docs/automations/wait-for-event) step's payload. |
| Static value | `"<string>"` | Passed as-is to the template. |

```json
{
  "key": "welcome",
  "type": "send_email",
  "config": {
    "template": {
      "id": "044db673-fff6-420f-a566-f6aba05d60e7",
      "variables": {
        "firstName": { "var": "event.firstName" },
        "orderNumber": { "var": "event.orderId" },
        "total": { "var": "event.amount" },
        "company": { "var": "contact.properties.company" },
        "feedback": { "var": "wait_events.feedback.received.response" },
        "supportEmail": "help@example.com"
      }
    }
  }
}
```

Template variables must exist in your referenced template and the key names must match the template variable names exactly.

> **Note:** If a branch has multiple [wait for event](https://www.mailblastr.com/docs/automations/wait-for-event) steps with the same key, the resolved data comes from the last event received before the current step.

## Configuration

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `config.template` | object | Yes | The published template to send. Provide `id` and optionally `variables`. |
| `config.template.id` | string | Yes | The ID or alias of the template to send. |
| `config.template.variables` | object | No | A key/value map of template variables. Each value is a static string or a variable reference (`{ "var": "event.field" }`) resolved from the `event.*`, `contact.*`, or `wait_events.*` namespaces. |
| `config.from` | string | No | The sender email address. Overrides the template default when provided. |
| `config.subject` | string | No | The email subject line. Overrides the template default when provided. |
| `config.reply_to` | string | No | The reply-to email address. Overrides the template default when provided. |

```json
{
  "key": "welcome",
  "type": "send_email",
  "config": {
    "template": {
      "id": "062f8ef4-fbfa-44f1-b5e0-ff8e1e8ffa96",
      "variables": {
        "name": { "var": "event.firstName" }
      }
    },
    "from": "hello@yourdomain.com",
    "subject": "Welcome!",
    "reply_to": "support@yourdomain.com"
  }
}
```
