# Reply to received emails

> Reply in-thread by setting In-Reply-To (and References) to the received message_id when sending via POST /emails.

Email clients **thread** messages using the `Message-ID` header. To reply to a received email so it lands in the same conversation, send a new email via [POST /emails](https://www.mailblastr.com/docs/api/emails-send) with an `In-Reply-To` header set to the original message's `message_id`. Start the subject with `Re:` so clients group the replies together.

## Get the message ID

The `message_id` is included in the [email.received](https://www.mailblastr.com/docs/receiving/introduction) webhook payload:

```json
{
  "type": "email.received",
  "data": {
    "email_id": "56761188-7520-42d8-8898-ff6fc54ce618",
    "message_id": "<111-222-333@email.example.com>",
    "subject": "Sending this example",
    "from": "Acme <onboarding@mailblastr.dev>",
    "to": ["delivered@mailblastr.dev"]
  }
}
```

Use `event.data.message_id` as the `In-Reply-To` header value when sending your reply.

## Send a reply in thread

Set the `In-Reply-To` header to the received `message_id` and prefix the subject with `Re:`. See [Custom headers](https://www.mailblastr.com/docs/emails/headers) for how MailBlastr writes custom MIME headers.

**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": ["delivered@example.com"],
  "subject": "Re: Sending this example",
  "html": "<p>Thanks for your email!</p>",
  "headers": {
    "In-Reply-To": "<111-222-333@email.example.com>"
  }
});
console.log({ data, error });
```

**Ruby**

```ruby
require "mailblastr"

Mailblastr.api_key = "mb_xxxxxxxxx"

Mailblastr::Emails.send({
  "from": "Acme <hello@yourdomain.com>",
  "to": [
    "delivered@example.com"
  ],
  "subject": "Re: Sending this example",
  "html": "<p>Thanks for your email!</p>",
  "headers": {
    "In-Reply-To": "<111-222-333@email.example.com>"
  }
})
```

**PHP**

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

$mailblastr->emails->send([
  'from' => "Acme <hello@yourdomain.com>",
  'to' => [
    "delivered@example.com"
  ],
  'subject' => "Re: Sending this example",
  'html' => "<p>Thanks for your email!</p>",
  'headers' => [
    'In-Reply-To' => "<111-222-333@email.example.com>"
  ]
]);
```

**Python**

```python
import mailblastr

mailblastr.api_key = "mb_xxxxxxxxx"

mailblastr.Emails.send({
  "from": "Acme <hello@yourdomain.com>",
  "to": [
    "delivered@example.com"
  ],
  "subject": "Re: Sending this example",
  "html": "<p>Thanks for your email!</p>",
  "headers": {
    "In-Reply-To": "<111-222-333@email.example.com>"
  }
})
```

**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": ["delivered@example.com"],
  "subject": "Re: Sending this example",
  "html": "<p>Thanks for your email!</p>",
  "headers": {
    "In-Reply-To": "<111-222-333@email.example.com>"
  }
}'
```

**CLI**

```bash
mailblastr emails send \
  --from 'Acme <hello@yourdomain.com>' \
  --to 'delivered@example.com' \
  --subject 'Re: Sending this example' \
  --html '<p>Thanks for your email!</p>' \
  --headers '{"In-Reply-To":"<111-222-333@email.example.com>"}'
```

## Reply headers

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `In-Reply-To` | string | Yes | The `message_id` of the message you are replying to (e.g. `<111-222-333@email.example.com>`). |
| `References` | string | No | Space-separated list of every prior `message_id` in the thread, ending with the one you are replying to. Used for deep threading across multiple replies. |

## Replying multiple times in a thread

When you reply more than once in the same conversation, also set the `References` header to all previous `message_id`s separated by spaces, ending with the current one. This helps clients keep the thread structure correct.

**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": ["delivered@example.com"],
  "subject": "Re: Sending this example",
  "html": "<p>Thanks for your email!</p>",
  "headers": {
    "In-Reply-To": "<111-222-333@email.example.com>",
    "References": "<msg_id1@domain.com> <msg_id2@domain.com> <111-222-333@email.example.com>"
  }
});
console.log({ data, error });
```

**Ruby**

```ruby
require "mailblastr"

Mailblastr.api_key = "mb_xxxxxxxxx"

Mailblastr::Emails.send({
  "from": "Acme <hello@yourdomain.com>",
  "to": [
    "delivered@example.com"
  ],
  "subject": "Re: Sending this example",
  "html": "<p>Thanks for your email!</p>",
  "headers": {
    "In-Reply-To": "<111-222-333@email.example.com>",
    "References": "<msg_id1@domain.com> <msg_id2@domain.com> <111-222-333@email.example.com>"
  }
})
```

**PHP**

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

$mailblastr->emails->send([
  'from' => "Acme <hello@yourdomain.com>",
  'to' => [
    "delivered@example.com"
  ],
  'subject' => "Re: Sending this example",
  'html' => "<p>Thanks for your email!</p>",
  'headers' => [
    'In-Reply-To' => "<111-222-333@email.example.com>",
    'References' => "<msg_id1@domain.com> <msg_id2@domain.com> <111-222-333@email.example.com>"
  ]
]);
```

**Python**

```python
import mailblastr

mailblastr.api_key = "mb_xxxxxxxxx"

mailblastr.Emails.send({
  "from": "Acme <hello@yourdomain.com>",
  "to": [
    "delivered@example.com"
  ],
  "subject": "Re: Sending this example",
  "html": "<p>Thanks for your email!</p>",
  "headers": {
    "In-Reply-To": "<111-222-333@email.example.com>",
    "References": "<msg_id1@domain.com> <msg_id2@domain.com> <111-222-333@email.example.com>"
  }
})
```

**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": ["delivered@example.com"],
  "subject": "Re: Sending this example",
  "html": "<p>Thanks for your email!</p>",
  "headers": {
    "In-Reply-To": "<111-222-333@email.example.com>",
    "References": "<msg_id1@domain.com> <msg_id2@domain.com> <111-222-333@email.example.com>"
  }
}'
```

**CLI**

```bash
mailblastr emails send \
  --from 'Acme <hello@yourdomain.com>' \
  --to 'delivered@example.com' \
  --subject 'Re: Sending this example' \
  --html '<p>Thanks for your email!</p>' \
  --headers '{"In-Reply-To":"<111-222-333@email.example.com>","References":"<msg_id1@domain.com> <msg_id2@domain.com> <111-222-333@email.example.com>"}'
```

> **Note:** The reply's `from` must be on a [verified sending domain](https://www.mailblastr.com/docs/domains/managing). The reply is an ordinary outbound send via `POST /emails`; the `In-Reply-To` / `References` headers are what place it in the existing thread.
