# Embed inline images

> Reference an attachment from your HTML with a cid: source so an image renders inline in the body of the email.

You can embed an image directly in the HTML body of an email instead of hosting it on an external server. The image travels with the message as an [attachment](https://www.mailblastr.com/docs/emails/attachments), and the HTML references it by a **content id** (`cid`).

> **Warning:** Inline images (and any attachment) are not supported on the [batch endpoint](https://www.mailblastr.com/docs/api/emails-batch). Use a single [POST /emails](https://www.mailblastr.com/docs/api/emails-send) when you need to embed an image.

## How it works

1. **Add a cid: reference in the HTML** — Use the `cid:` prefix in the `src` of an `<img>` tag, e.g. `<img src="cid:logo-image">`. The value after `cid:` is an arbitrary id you choose; it must be under 128 characters.
2. **Set content_id on the attachment** — Add the same id as the `content_id` field of the matching attachment object. MailBlastr links the `<img>` to the attachment by this id when it assembles the message.

## Remote image

Reference a hosted image with `path`. MailBlastr fetches and base64-encodes it for you at send time (the fetch is SSRF-guarded), so you do not need to encode anything yourself.

**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": "Thank you for contacting us",
  "html": "<p>Here is our <img src=\"cid:logo-image\"/> inline logo</p>",
  "attachments": [
    {
      "path": "https://files.example.com/logo.png",
      "filename": "logo.png",
      "content_id": "logo-image"
    }
  ]
});
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": "Thank you for contacting us",
  "html": "<p>Here is our <img src=\"cid:logo-image\"/> inline logo</p>",
  "attachments": [
    {
      "path": "https://files.example.com/logo.png",
      "filename": "logo.png",
      "content_id": "logo-image"
    }
  ]
})
```

**PHP**

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

$mailblastr->emails->send([
  'from' => "Acme <hello@yourdomain.com>",
  'to' => [
    "delivered@example.com"
  ],
  'subject' => "Thank you for contacting us",
  'html' => "<p>Here is our <img src=\"cid:logo-image\"/> inline logo</p>",
  'attachments' => [
    [
      'path' => "https://files.example.com/logo.png",
      'filename' => "logo.png",
      'content_id' => "logo-image"
    ]
  ]
]);
```

**Python**

```python
import mailblastr

mailblastr.api_key = "mb_xxxxxxxxx"

mailblastr.Emails.send({
  "from": "Acme <hello@yourdomain.com>",
  "to": [
    "delivered@example.com"
  ],
  "subject": "Thank you for contacting us",
  "html": "<p>Here is our <img src=\"cid:logo-image\"/> inline logo</p>",
  "attachments": [
    {
      "path": "https://files.example.com/logo.png",
      "filename": "logo.png",
      "content_id": "logo-image"
    }
  ]
})
```

**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": "Thank you for contacting us",
  "html": "<p>Here is our <img src=\"cid:logo-image\"/> inline logo</p>",
  "attachments": [
    {
      "path": "https://files.example.com/logo.png",
      "filename": "logo.png",
      "content_id": "logo-image"
    }
  ]
}'
```

## Local image

To inline a file from disk, read it and base64-encode the bytes into the `content` field, then point the `content_id` at the matching `cid`.

**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": "Thank you for contacting us",
  "html": "<p>Here is our <img src=\"cid:logo-image\"/> inline logo</p>",
  "attachments": [
    {
      "content": "iVBORw0KGgoAAAANS...base64...",
      "filename": "logo.png",
      "content_type": "image/png",
      "content_id": "logo-image"
    }
  ]
});
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": "Thank you for contacting us",
  "html": "<p>Here is our <img src=\"cid:logo-image\"/> inline logo</p>",
  "attachments": [
    {
      "content": "iVBORw0KGgoAAAANS...base64...",
      "filename": "logo.png",
      "content_type": "image/png",
      "content_id": "logo-image"
    }
  ]
})
```

**PHP**

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

$mailblastr->emails->send([
  'from' => "Acme <hello@yourdomain.com>",
  'to' => [
    "delivered@example.com"
  ],
  'subject' => "Thank you for contacting us",
  'html' => "<p>Here is our <img src=\"cid:logo-image\"/> inline logo</p>",
  'attachments' => [
    [
      'content' => "iVBORw0KGgoAAAANS...base64...",
      'filename' => "logo.png",
      'content_type' => "image/png",
      'content_id' => "logo-image"
    ]
  ]
]);
```

**Python**

```python
import mailblastr

mailblastr.api_key = "mb_xxxxxxxxx"

mailblastr.Emails.send({
  "from": "Acme <hello@yourdomain.com>",
  "to": [
    "delivered@example.com"
  ],
  "subject": "Thank you for contacting us",
  "html": "<p>Here is our <img src=\"cid:logo-image\"/> inline logo</p>",
  "attachments": [
    {
      "content": "iVBORw0KGgoAAAANS...base64...",
      "filename": "logo.png",
      "content_type": "image/png",
      "content_id": "logo-image"
    }
  ]
})
```

**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": "Thank you for contacting us",
  "html": "<p>Here is our <img src=\"cid:logo-image\"/> inline logo</p>",
  "attachments": [
    {
      "content": "iVBORw0KGgoAAAANS...base64...",
      "filename": "logo.png",
      "content_type": "image/png",
      "content_id": "logo-image"
    }
  ]
}'
```

## Things to keep in mind

- When you inline a **local** image you must base64-encode it yourself in `content`. With a **remote** `path`, MailBlastr fetches and encodes it for you.
- Inline images increase the total size of the email — they count toward the per-email (40 MB) [size limit](https://www.mailblastr.com/docs/emails/attachments).
- Some clients (especially webmail) may reject or strip inline images. Where it matters, host the image and link to it normally instead.
- Set a `content_type` (e.g. `image/png`) or a `filename` (e.g. `logo.png`) so receiving clients render the image correctly.

> **Note:** All [attachment requirements and limits](https://www.mailblastr.com/docs/emails/attachments) apply to inline images, since an inline image is just an attachment referenced from the HTML.
