> ## Documentation Index
> Fetch the complete documentation index at: https://docs.emailr.dev/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Attachments

> Send emails with attachments.

There are two ways to send attachments:

1. [From a remote file](#send-attachments-from-a-remote-file)
2. [From a local file](#send-attachments-from-a-local-file)

<Info>
  We currently do not support sending attachments when using our batch endpoint.
</Info>

## Send attachments from a remote file

Include the `path` parameter to send attachments from a remote file. This parameter accepts a URL to the file you want to attach.

Define the file name that will be attached using the `filename` parameter.

<CodeGroup>
  ```ts Node.js {12-13} theme={null} theme={null}
  import { Emailr } from 'emailr';

  const emailr = new Emailr('em_xxxxxxxxx');

  await emailr.emails.send({
    from: 'Acme <onboarding@emailr.dev>',
    to: ['delivered@emailr.dev'],
    subject: 'Receipt for your payment',
    html: '<p>Thanks for the payment</p>',
    attachments: [
      {
        path: 'https://emailr.dev/static/sample/invoice.pdf',
        filename: 'invoice.pdf',
      },
    ],
  });
  ```

  ```php PHP {10-11} theme={null} theme={null}
  $emailr = Emailr::client('em_xxxxxxxxx');

  $emailr->emails->send([
    'from' => 'Acme <onboarding@emailr.dev>',
    'to' => ['delivered@emailr.dev'],
    'subject' => 'Receipt for your payment',
    'html' => '<p>Thanks for the payment</p>',
    'attachments' => [
      [
        'path' => 'https://emailr.dev/static/sample/invoice.pdf',
        'filename' => 'invoice.pdf'
      ]
    ]
  ]);
  ```

  ```python Python {6-7} theme={null} theme={null}
  import emailr

  emailr.api_key = "em_xxxxxxxxx"

  attachment: emailr.RemoteAttachment = {
    "path": "https://emailr.dev/static/sample/invoice.pdf",
    "filename": "invoice.pdf",
  }

  params: emailr.Emails.SendParams = {
    "from": "Acme <onboarding@emailr.dev>",
    "to": ["delivered@emailr.dev"],
    "subject": "Receipt for your payment",
    "html": "<p>Thanks for the payment</p>",
    "attachments": [attachment],
  }

  emailr.Emails.send(params)
  ```

  ```rb Ruby {12-13} theme={null} theme={null}
  require "emailr"

  Emailr.api_key = "em_xxxxxxxxx"

  params = {
    "from": "Acme <onboarding@emailr.dev>",
    "to": ["delivered@emailr.dev"],
    "subject": "Receipt for your payment",
    "html": "<p>Thanks for the payment</p>",
    "attachments": [
      {
        "path": "https://emailr.dev/static/sample/invoice.pdf",
        "filename": 'invoice.pdf',
      }
    ]
  }

  Emailr::Emails.send(params)
  ```

  ```go Go {12-13} theme={null} theme={null}
  import (
  	"fmt"

  	"github.com/emailr/emailr-go/v3"
  )

  func main() {
    ctx := context.TODO()
    client := emailr.NewClient("em_xxxxxxxxx")

    attachment := &emailr.Attachment{
      Path:  "https://emailr.dev/static/sample/invoice.pdf",
      Filename: "invoice.pdf",
    }

    params := &emailr.SendEmailRequest{
        From:        "Acme <onboarding@emailr.dev>",
        To:          []string{"delivered@emailr.dev"},
        Subject:     "Receipt for your payment",
        Html:        "<p>Thanks for the payment</p>",
        Attachments: []*emailr.Attachment{attachment},
    }

    sent, err := client.Emails.SendWithContext(ctx, params)

    if err != nil {
      panic(err)
    }
    fmt.Println(sent.Id)
  }
  ```

  ```rust Rust {12-13} theme={null} theme={null}
  use emailr_rs::types::{CreateAttachment, CreateEmailBaseOptions};
  use emailr_rs::{Emailr, Result};

  #[tokio::main]
  async fn main() -> Result<()> {
    let emailr = Emailr::new("em_xxxxxxxxx");

    let from = "Acme <onboarding@emailr.dev>";
    let to = ["delivered@emailr.dev"];
    let subject = "Receipt for your payment";

    let path = "https://emailr.dev/static/sample/invoice.pdf";
    let filename = "invoice.pdf";

    let email = CreateEmailBaseOptions::new(from, to, subject)
      .with_html("<p>Thanks for the payment</p>")
      .with_attachment(CreateAttachment::from_path(path).with_filename(filename));

    let _email = emailr.emails.send(email).await?;

    Ok(())
  }
  ```

  ```java Java {8-9} theme={null} theme={null}
  import com.emailr.*;

  public class Main {
      public static void main(String[] args) {
          Emailr emailr = new Emailr("em_xxxxxxxxx");

          Attachment att = Attachment.builder()
                  .path("https://emailr.dev/static/sample/invoice.pdf")
                  .fileName("invoice.pdf")
                  .build();

          CreateEmailOptions params = CreateEmailOptions.builder()
                  .from("Acme <onboarding@emailr.dev>")
                  .to("delivered@emailr.dev")
                  .subject("Receipt for your payment")
                  .html("<p>Thanks for the payment</p>")
                  .attachments(att)
                  .build();

          CreateEmailResponse data = emailr.emails().send(params);
      }
  }
  ```

  ```csharp .NET {14-18} theme={null} theme={null}
  using Emailr;
  using System.Collections.Generic;

  IEmailr emailr = EmailrClient.Create( "em_xxxxxxxxx" ); // Or from DI

  var message = new EmailMessage()
  {
      From = "Acme <onboarding@emailr.dev>",
      To = "delivered@emailr.dev",
      Subject = "Receipt for your payment",
      HtmlBody = "<p>Thanks for the payment</p>",
  };

  message.Attachments = new List<EmailAttachment>();
  message.Attachments.Add( new EmailAttachment() {
    Filename = "invoice.pdf",
    Path = "https://emailr.dev/static/sample/invoice.pdf",
  } );

  var resp = await emailr.EmailSendAsync( message );
  Console.WriteLine( "Email Id={0}", resp.Content );
  ```

  ```bash cURL {11-12} theme={null} theme={null}
  curl -X POST 'https://api.emailr.dev/emails' \
       -H 'Authorization: Bearer em_xxxxxxxxx' \
       -H 'Content-Type: application/json' \
       -d $'{
    "from": "Acme <onboarding@emailr.dev>",
    "to": ["delivered@emailr.dev"],
    "subject": "Receipt for your payment",
    "html": "<p>Thanks for the payment</p>",
    "attachments": [
      {
        "path": "https://emailr.dev/static/sample/invoice.pdf",
        "filename": "invoice.pdf"
      }
    ]
  }'
  ```
</CodeGroup>

## Send attachments from a local file

Include the `content` parameter to send attachments from a local file. This parameter accepts the Base64 encoded content of the file you want to attach.

Define the file name that will be attached using the `filename` parameter.

<CodeGroup>
  ```ts Node.js {16-17} theme={null} theme={null}
  import { Emailr } from 'emailr';
  import fs from 'fs';

  const emailr = new Emailr('em_xxxxxxxxx');

  const filepath = `${__dirname}/static/invoice.pdf`;
  const attachment = fs.readFileSync(filepath).toString('base64');

  await emailr.emails.send({
    from: 'Acme <onboarding@emailr.dev>',
    to: ['delivered@emailr.dev'],
    subject: 'Receipt for your payment',
    text: '<p>Thanks for the payment</p>',
    attachments: [
      {
        content: attachment,
        filename: 'invoice.pdf',
      },
    ],
  });
  ```

  ```php PHP {10-11} theme={null} theme={null}
  $emailr = Emailr::client('em_xxxxxxxxx');

  $emailr->emails->send([
    'from' => 'Acme <onboarding@emailr.dev>',
    'to' => ['delivered@emailr.dev'],
    'subject' => 'Receipt for your payment',
    'html' => '<p>Thanks for the payment</p>',
    'attachments' => [
      [
        'filename' => 'invoice.pdf',
        'content' => $invoiceBuffer
      ]
    ]
  ]);
  ```

  ```python Python {10} theme={null} theme={null}
  import os
  import emailr

  emailr.api_key = "em_xxxxxxxxx"

  f: bytes = open(
    os.path.join(os.path.dirname(__file__), "../static/invoice.pdf"), "rb"
  ).read()

  attachment: emailr.Attachment = {"content": list(f), "filename": "invoice.pdf"}

  params: emailr.Emails.SendParams = {
    "from": "Acme <onboarding@emailr.dev>",
    "to": ["delivered@emailr.dev"],
    "subject": "Receipt for your payment",
    "html": "<p>Thanks for the payment</p>",
    "attachments": [attachment],
  }

  emailr.Emails.send(params)
  ```

  ```rb Ruby {14-15} theme={null} theme={null}
  require "emailr"

  Emailr.api_key = "em_xxxxxxxxx"

  file = IO.read("invoice.pdf")

  params = {
    "from": "Acme <onboarding@emailr.dev>",
    "to": ["delivered@emailr.dev"],
    "subject": "Receipt for your payment",
    "html": "<p>Thanks for the payment</p>",
    "attachments": [
      {
        "content": file.bytes,
        "filename": 'invoice.pdf',
      }
    ]
  }

  Emailr::Emails.send(params)
  ```

  ```go Go {19-20} theme={null} theme={null}
  import (
  	"fmt"
  	"os"

  	"github.com/emailr/emailr-go/v3"
  )

  func main() {
    ctx := context.TODO()
    client := emailr.NewClient("em_xxxxxxxxx")

    pwd, _ := os.Getwd()
    f, err := os.ReadFile(pwd + "/static/invoice.pdf")
    if err != nil {
      panic(err)
    }

    attachment := &emailr.Attachment{
      Content:  f,
      Filename: "invoice.pdf",
    }

    params := &emailr.SendEmailRequest{
        From:        "Acme <onboarding@emailr.dev>",
        To:          []string{"delivered@emailr.dev"},
        Subject:     "Receipt for your payment",
        Html:        "<p>Thanks for the payment</p>",
        Attachments: []*emailr.Attachment{attachment},
    }

    sent, err := client.Emails.SendWithContext(ctx, params)

    if err != nil {
      panic(err)
    }
    fmt.Println(sent.Id)
  }
  ```

  ```rust Rust {22} theme={null} theme={null}
  use std::fs::File;
  use std::io::Read;

  use emailr_rs::types::{CreateAttachment, CreateEmailBaseOptions};
  use emailr_rs::{Emailr, Result};

  #[tokio::main]
  async fn main() -> Result<()> {
    let emailr = Emailr::new("em_xxxxxxxxx");

    let from = "Acme <onboarding@emailr.dev>";
    let to = ["delivered@emailr.dev"];
    let subject = "Receipt for your payment";

    let filename = "invoice.pdf";
    let mut f = File::open(filename).unwrap();
    let mut invoice = Vec::new();
    f.read_to_end(&mut invoice).unwrap();

    let email = CreateEmailBaseOptions::new(from, to, subject)
      .with_html("<p>Thanks for the payment</p>")
      .with_attachment(CreateAttachment::from_content(invoice).with_filename(filename));

    let _email = emailr.emails.send(email).await?;

    Ok(())
  }
  ```

  ```java Java {8-9} theme={null} theme={null}
  import com.emailr.*;

  public class Main {
      public static void main(String[] args) {
          Emailr emailr = new Emailr("em_xxxxxxxxx");

          Attachment att = Attachment.builder()
                  .fileName("invoice.pdf")
                  .content("invoiceBuffer")
                  .build();

          CreateEmailOptions params = CreateEmailOptions.builder()
                  .from("Acme <onboarding@emailr.dev>")
                  .to("delivered@emailr.dev")
                  .subject("Receipt for your payment")
                  .html("<p>Thanks for the payment</p>")
                  .attachments(att)
                  .build();

          CreateEmailOptions params = CreateEmailOptions.builder()
      }
  }
  ```

  ```csharp .NET {15-19} theme={null} theme={null}
  using Emailr;
  using System.Collections.Generic;
  using System.IO;

  IEmailr emailr = EmailrClient.Create( "em_xxxxxxxxx" ); // Or from DI

  var message = new EmailMessage()
  {
      From = "Acme <onboarding@emailr.dev>",
      To = "delivered@emailr.dev",
      Subject = "Receipt for your payment",
      HtmlBody = "<p>Thanks for the payment</p>",
  };

  message.Attachments = new List<EmailAttachment>();
  message.Attachments.Add( new EmailAttachment() {
    Filename = "invoice.pdf",
    Content = await File.ReadAllBytesAsync( "invoice.pdf" ),
  } );

  var resp = await emailr.EmailSendAsync( message );
  Console.WriteLine( "Email Id={0}", resp.Content );
  ```

  ```bash cURL {11-12} theme={null} theme={null}
  curl -X POST 'https://api.emailr.dev/emails' \
       -H 'Authorization: Bearer em_xxxxxxxxx' \
       -H 'Content-Type: application/json' \
       -d $'{
    "from": "Acme <onboarding@emailr.dev>",
    "to": ["delivered@emailr.dev"],
    "subject": "Receipt for your payment",
    "html": "<p>Thanks for the payment</p>",
    "attachments": [
      {
        "content": "UmVzZW5kIGF0dGFjaG1lbnQgZXhhbXBsZS4gTmljZSBqb2Igc2VuZGluZyB0aGUgZW1haWwh%",
        "filename": "invoice.txt"
      }
    ]
  }'
  ```
</CodeGroup>

## Embed Images using CID

You can optionally embed an image in the HTML body of the email. Both remote and local attachments are supported. All attachment requirements, options, and limitations apply to embedded inline images as well.

Embedding images requires two steps:

**1. Add the CID in the email HTML.**

Use the prefix `cid:` to reference the ID in the `src` attribute of an image tag in the HTML body of the email.

```html theme={null} theme={null}
<img src="cid:logo-image" />
```

**2. Reference the CID in the attachment**

The content id is an arbitrary string set by you, and must be less than 128 characters.

<CodeGroup>
  ```ts Node.js {9, 14} theme={null} theme={null}
  import { Emailr } from 'emailr';

  const emailr = new Emailr('em_xxxxxxxxx');

  await emailr.emails.send({
    from: 'Acme <onboarding@emailr.dev>',
    to: ['delivered@emailr.dev'],
    subject: 'Thank you for contacting us',
    html: '<p>Here is our <img src="cid:logo-image"/> inline logo</p>',
    attachments: [
      {
        path: 'https://emailr.dev/static/sample/logo.png',
        filename: 'logo.png',
        contentId: 'logo-image',
      },
    ],
  });
  ```

  ```php PHP {7,12} theme={null} theme={null}
  $emailr = Emailr::client('em_xxxxxxxxx');

  $emailr->emails->send([
    'from' => 'Acme <onboarding@emailr.dev>',
    'to' => ['delivered@emailr.dev'],
    'subject' => 'Thank you for contacting us',
    'html' => '<p>Here is our <img src="cid:logo-image"/> inline logo</p>',
    'attachments' => [
      [
        'path' => 'https://emailr.dev/static/sample/logo.png',
        'filename' => 'logo.png',
        'content_id' => 'logo-image',
      ]
    ]
  ]);
  ```

  ```python Python {8,15} theme={null} theme={null}
  import emailr

  emailr.api_key = "em_xxxxxxxxx"

  attachment: emailr.RemoteAttachment = {
    "path": "https://emailr.dev/static/sample/logo.png",
    "filename": "logo.png",
    "content_id": "logo-image",
  }

  params: emailr.Emails.SendParams = {
    "from": "Acme <onboarding@emailr.dev>",
    "to": ["delivered@emailr.dev"],
    "subject": "Thank you for contacting us",
    "html": "<p>Here is our <img src=\"cid:logo-image\"/> inline logo</p>",
    "attachments": [attachment],
  }

  emailr.Emails.send(params)
  ```

  ```rb Ruby {9, 14} theme={null} theme={null}
  require "emailr"

  Emailr.api_key = "em_xxxxxxxxx"

  params = {
    "from": "Acme <onboarding@emailr.dev>",
    "to": ["delivered@emailr.dev"],
    "subject": "Thank you for contacting us",
    "html": "<p>Here is our <img src=\"cid:logo-image\"/> inline logo</p>",
    "attachments": [
      {
        "path": "https://emailr.dev/static/sample/logo.png",
        "filename": 'logo.png',
        "content_id": "logo-image",
      }
    ]
  }

  Emailr::Emails.send(params)
  ```

  ```go Go {14, 21} theme={null} theme={null}
  import (
  	"fmt"

  	"github.com/emailr/emailr-go/v3"
  )

  func main() {
    ctx := context.TODO()
    client := emailr.NewClient("em_xxxxxxxxx")

    attachment := &emailr.Attachment{
      Path:  "https://emailr.dev/static/sample/logo.png",
      Filename: "logo.png",
      ContentId: "logo-image",
    }

    params := &emailr.SendEmailRequest{
        From:        "Acme <onboarding@emailr.dev>",
        To:          []string{"delivered@emailr.dev"},
        Subject:     "Thank you for contacting us",
        Html:        "<p>Here is our <img src=\"cid:logo-image\"/> inline logo</p>",
        Attachments: []*emailr.Attachment{attachment},
    }

    sent, err := client.Emails.SendWithContext(ctx, params)

    if err != nil {
      panic(err)
    }
    fmt.Println(sent.Id)
  }
  ```

  ```rust Rust {14, 17} theme={null} theme={null}
  use emailr_rs::types::{CreateAttachment, CreateEmailBaseOptions};
  use emailr_rs::{Emailr, Result};

  #[tokio::main]
  async fn main() -> Result<()> {
    let emailr = Emailr::new("em_xxxxxxxxx");

    let from = "Acme <onboarding@emailr.dev>";
    let to = ["delivered@emailr.dev"];
    let subject = "Thank you for contacting us";

    let path = "https://emailr.dev/static/sample/logo.png";
    let filename = "logo.png";
    let content_id = "logo-image";

    let email = CreateEmailBaseOptions::new(from, to, subject)
      .with_html("<p>Here is our <img src=\"cid:logo-image\"/> inline logo</p>")
      .with_attachment(
        CreateAttachment::from_path(path)
          .with_filename(filename)
          .with_content_id(content_id),
      );

    let _email = emailr.emails.send(email).await?;

    Ok(())
  }
  ```

  ```java Java {10, 17} theme={null} theme={null}
  import com.emailr.*;

  public class Main {
      public static void main(String[] args) {
          Emailr emailr = new Emailr("em_xxxxxxxxx");

          Attachment att = Attachment.builder()
                  .path("https://emailr.dev/static/sample/logo.png")
                  .fileName("logo.png")
                  .ContentId("logo-image")
                  .build();

          CreateEmailOptions params = CreateEmailOptions.builder()
                  .from("Acme <onboarding@emailr.dev>")
                  .to("delivered@emailr.dev")
                  .subject("Thank you for contacting us")
                  .html("<p>Here is our <img src=\"cid:logo-image\"/> inline logo</p>")
                  .attachments(att)
                  .build();

          CreateEmailResponse data = emailr.emails().send(params);
      }
  }
  ```

  ```csharp .NET {11, 18} theme={null} theme={null}
  using Emailr;
  using System.Collections.Generic;

  IEmailr emailr = EmailrClient.Create( "em_xxxxxxxxx" ); // Or from DI

  var message = new EmailMessage()
  {
      From = "Acme <onboarding@emailr.dev>",
      To = "delivered@emailr.dev",
      Subject = "Thank you for contacting us",
      HtmlBody = "<p>Here is our <img src=\"cid:logo-image\"/> inline logo</p>",
  };

  message.Attachments = new List<EmailAttachment>();
  message.Attachments.Add( new EmailAttachment() {
    Filename = "logo.png",
    Path = "https://emailr.dev/static/sample/logo.png",
    ContentId = "logo-image",
  } );

  var resp = await emailr.EmailSendAsync( message );
  Console.WriteLine( "Email Id={0}", resp.Content );
  ```

  ```bash cURL {8,13} theme={null} theme={null}
  curl -X POST 'https://api.emailr.dev/emails' \
       -H 'Authorization: Bearer em_xxxxxxxxx' \
       -H 'Content-Type: application/json' \
       -d $'{
    "from": "Acme <onboarding@emailr.dev>",
    "to": ["delivered@emailr.dev"],
    "subject": "Thank you for contacting us",
    "html": "<p>Here is our <img src=\"cid:logo-image\"/> inline logo</p>",
    "attachments": [
      {
        "path": "https://emailr.dev/static/sample/logo.png",
        "filename": "logo.png",
        "content_id": "logo-image"
      }
    ]
  }'
  ```
</CodeGroup>

Learn more about [embedding images](/docs/guides/dashboard/emails/embed-inline-images).

## View and Download Attachments

You can view and download attachments when viewing a sent email that includes them.

To view and download attachments:

1. Go to [Emails](https://app.emailr.dev/dashboard/emails).
2. Navigate to any email you sent with an attachment.
3. Click on the attachment to download it locally.

Attachments include the filename and an icon to help you identify the type of attachment. We show unique icons for each attachment type:

* Image
* PDF
* Spreadsheet
* Default (for unknown types)

## Attachment Limitations

* Emails can be no larger than 40MB (including attachments after Base64 encoding).
* Not all file types are supported. See the list of [unsupported file types](/docs/guides/knowledge-base/what-attachment-types-are-not-supported).
* Emails with attachments cannot be sent using our batch endpoint.

## Examples

<CardGroup>
  <Card title="Attachments with Next.js (remote file)" icon="arrow-up-right-from-square" href="https://github.com/emailr/emailr-examples/tree/main/with-attachments">
    See the full source code.
  </Card>

  <Card title="Attachments with Next.js (local file)" icon="arrow-up-right-from-square" href="https://github.com/emailr/emailr-examples/tree/main/with-attachments-content">
    See the full source code.
  </Card>
</CardGroup>
