# Send Email

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

The **send email** step sends a [template](https://www.sendping.co/docs/templates/overview) email to the contact running through the automation. After adding a [trigger](https://www.sendping.co/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.sendping.co/docs/automations/overview) for the wider flow.

## How it works

**Node.js**

```js
import { SendPing } from 'sendping';

const mb = new SendPing('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 "sendping"

SendPing.api_key = "mb_xxxxxxxxx"

SendPing::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
<?php
require 'vendor/autoload.php';

use SendPing\SendPing;

$sendping = SendPing::client('mb_xxxxxxxxx');

$sendping->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 sendping

sendping.api_key = "mb_xxxxxxxxx"

sendping.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
import "github.com/shekhu10/sendping-sdks/sendping-go"

client := sendping.NewClient("mb_xxxxxxxxx")

automation, err := client.Automations.Create(&sendping.CreateAutomationRequest{
    Name:   "Welcome series",
    Domain: "yourdomain.com",
    Steps: []sendping.AutomationStepInput{
        {
            Key:    "start",
            Type:   "trigger",
            Config: map[string]any{"event_name": "user.created"},
        },
        {
            Key:  "welcome",
            Type: "send_email",
            Config: map[string]any{
                "template": map[string]any{"id": "044db673-fff6-420f-a566-f6aba05d60e7"},
            },
        },
    },
    Connections: []sendping.AutomationConnectionInput{{From: "start", To: "welcome"}},
})
```

**Rust**

```rust
use sendping::{AutomationStepInput, ConnectionInput, CreateAutomationOptions, SendPing};

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

let params = CreateAutomationOptions::new("Welcome series", "yourdomain.com")
    .with_step(AutomationStepInput::new("trigger")
        .with_key("start")
        .with_config(serde_json::json!({"event_name":"user.created"})))
    .with_step(AutomationStepInput::new("send_email")
        .with_key("welcome")
        .with_config(serde_json::json!({
            "template": {
                "id": "044db673-fff6-420f-a566-f6aba05d60e7"
            }
        })))
    .with_connection(ConnectionInput::new("start", "welcome"));
let _automation = mb.automations.create(params).await?;
```

**Java**

```java
import co.sendping.SendPing;
import co.sendping.SendPingResponse;
import co.sendping.requests.AutomationConnection;
import co.sendping.requests.AutomationStep;
import co.sendping.requests.CreateAutomationRequest;
import java.util.Map;

SendPing sendping = new SendPing("mb_xxxxxxxxx");

CreateAutomationRequest request = CreateAutomationRequest.builder()
        .name("Welcome series")
        .domain("yourdomain.com")
        .step(AutomationStep.builder()
                .key("start")
                .type("trigger")
                .config("event_name", "user.created")
                .build())
        .step(AutomationStep.builder()
                .key("welcome")
                .type("send_email")
                .config("template", Map.of("id", "044db673-fff6-420f-a566-f6aba05d60e7"))
                .build())
        .connection(AutomationConnection.of("start", "welcome"))
        .build();

SendPingResponse response = sendping.automations().create(request);
```

**.NET**

```csharp
using SendPing;

ISendPing sendping = SendPingClient.Create("mb_xxxxxxxxx");

var resp = await sendping.AutomationCreateAsync(new AutomationCreateOptions
{
    Name = "Welcome series",
    Domain = "yourdomain.com",
    Steps = new List<AutomationStepInput>
    {
        new()
        {
            Key = "start",
            Type = "trigger",
            Config = new Dictionary<string, object?>
            {
                ["event_name"] = "user.created",
            },
        },
        new()
        {
            Key = "welcome",
            Type = "send_email",
            Config = new Dictionary<string, object?>
            {
                ["template"] = new Dictionary<string, object?>
                {
                    ["id"] = "044db673-fff6-420f-a566-f6aba05d60e7",
                },
            },
        },
    },
    Connections = new List<AutomationConnection>
    {
        new() { From = "start", To = "welcome" },
    },
});
```

**cURL**

```bash
curl -X POST 'https://www.sendping.co/api/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.sendping.co/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.sendping.co/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"
  }
}
```
