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

# Send with Attachments

> Learn how to send a transactional email with file attachments

You can send transactional emails with one or more file attachments using the **thePurplebox API**.\
This is useful for delivering invoices, reports, receipts, or any other documents generated by your application.

## Prerequisites

* A thePurplebox account
* A secret API key generated from your dashboard
* A verified sending domain (recommended for best deliverability)

## Parameters

When sending an email with attachments, include the following in your request:

* **to** — A single email address or array of recipient emails
* **subject** — The email subject line
* **body** — Plain text or HTML content for the email body
* **attachments** — An array of attachment objects (up to 5 files):
  * **filename** — The name of the file as it will appear to the recipient
  * **content** — Base64-encoded file data
  * **contentType** — MIME type (e.g., `application/pdf`, `image/png`)

## Important Notes

* You can attach **up to 5 files per email**
* The `content` field **must be base64 encoded**
* Maximum attachment size per file is **5MB**
* Total email size (including all attachments) must not exceed **25MB**

## Example Requests

<Tabs>
  <Tab label="JavaScript" title="JavaScript" icon="js">
    ```javascript theme={null}
    await fetch('https://api.thepurplebox.io/v1/send', {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "Authorization": "Bearer <API_KEY>"
      },
      body: JSON.stringify({
        to: "user@example.com",
        subject: "Your requested document",
        body: "Please find the attached file.",
        attachments: [
          {
            filename: "document.pdf",
            content: "<BASE64_ENCODED_CONTENT>",
            contentType: "application/pdf"
          }
        ]
      })
    })
    ```
  </Tab>

  <Tab label="Go" title="Go" icon="golang">
    ```go theme={null}
    package main

    import (
    	"bytes"
    	"encoding/json"
    	"net/http"
    )

    func main() {
    	payload := map[string]interface{}{
    		"to":      "user@example.com",
    		"subject": "Your requested document",
    		"body":    "Please find the attached file.",
    		"attachments": []map[string]string{
    			{
    				"filename":    "document.pdf",
    				"content":     "<BASE64_ENCODED_CONTENT>",
    				"contentType": "application/pdf",
    			},
    		},
    	}

    	jsonData, _ := json.Marshal(payload)
    	req, _ := http.NewRequest("POST", "https://api.thepurplebox.io/v1/send", bytes.NewBuffer(jsonData))
    	req.Header.Set("Authorization", "Bearer <API_KEY>")
    	req.Header.Set("Content-Type", "application/json")

    	client := &http.Client{}
    	client.Do(req)
    }
    ```
  </Tab>

  <Tab label="Python" title="Python" icon="python">
    ```python theme={null}
    import requests

    url = "https://api.thepurplebox.io/v1/send"

    payload = {
        "to": "user@example.com",
        "subject": "Your requested document",
        "body": "Please find the attached file.",
        "attachments": [
            {
                "filename": "document.pdf",
                "content": "<BASE64_ENCODED_CONTENT>",
                "contentType": "application/pdf"
            }
        ]
    }

    headers = {
        "Authorization": "Bearer <API_KEY>",
        "Content-Type": "application/json"
    }

    requests.post(url, json=payload, headers=headers)
    ```
  </Tab>

  <Tab label="PHP" title="PHP" icon="php">
    ```php theme={null}
    <?php

    $url = "https://api.thepurplebox.io/v1/send";

    $payload = [
        "to" => "user@example.com",
        "subject" => "Your requested document",
        "body" => "Please find the attached file.",
        "attachments" => [
            [
                "filename" => "document.pdf",
                "content" => "<BASE64_ENCODED_CONTENT>",
                "contentType" => "application/pdf"
            ]
        ]
    ];

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Authorization: Bearer <API_KEY>",
        "Content-Type: application/json"
    ]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    curl_close($ch);
    ?>
    ```
  </Tab>

  <Tab label="Java" title="Java" icon="java">
    ```java theme={null}
    import java.net.http.HttpClient;
    import java.net.http.HttpRequest;
    import java.net.http.HttpResponse;
    import java.net.URI;

    public class SendEmail {
        public static void main(String[] args) throws Exception {
            String json = """
            {
                "to": "user@example.com",
                "subject": "Your requested document",
                "body": "Please find the attached file.",
                "attachments": [
                    {
                        "filename": "document.pdf",
                        "content": "<BASE64_ENCODED_CONTENT>",
                        "contentType": "application/pdf"
                    }
                ]
            }
            """;

            HttpClient client = HttpClient.newHttpClient();
            HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("https://api.thepurplebox.io/v1/send"))
                .header("Content-Type", "application/json")
                .header("Authorization", "Bearer <API_KEY>")
                .POST(HttpRequest.BodyPublishers.ofString(json))
                .build();

            HttpResponse<String> response = client.send(request, 
                HttpResponse.BodyHandlers.ofString());
        }
    }
    ```
  </Tab>

  <Tab label="Ruby" title="Ruby">
    ```ruby theme={null}
    require 'net/http'
    require 'json'
    require 'uri'

    uri = URI('https://api.thepurplebox.io/v1/send')
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true

    request = Net::HTTP::Post.new(uri)
    request['Content-Type'] = 'application/json'
    request['Authorization'] = 'Bearer <API_KEY>'

    request.body = {
      to: 'user@example.com',
      subject: 'Your requested document',
      body: 'Please find the attached file.',
      attachments: [
        {
          filename: 'document.pdf',
          content: '<BASE64_ENCODED_CONTENT>',
          contentType: 'application/pdf'
        }
      ]
    }.to_json

    response = http.request(request)
    ```
  </Tab>

  <Tab label="C#" title="C#">
    ```csharp theme={null}
    using System;
    using System.Net.Http;
    using System.Text;
    using System.Text.Json;
    using System.Threading.Tasks;

    public class SendEmail
    {
        public static async Task Main()
        {
            var client = new HttpClient();
            client.DefaultRequestHeaders.Add("Authorization", "Bearer <API_KEY>");

            var payload = new
            {
                to = "user@example.com",
                subject = "Your requested document",
                body = "Please find the attached file.",
                attachments = new[]
                {
                    new
                    {
                        filename = "document.pdf",
                        content = "<BASE64_ENCODED_CONTENT>",
                        contentType = "application/pdf"
                    }
                }
            };

            var json = JsonSerializer.Serialize(payload);
            var content = new StringContent(json, Encoding.UTF8, "application/json");

            var response = await client.PostAsync(
                "https://api.thepurplebox.io/v1/send", 
                content
            );
        }
    }
    ```
  </Tab>

  <Tab label="ASP.NET" title="ASP.NET">
    ```csharp theme={null}
    using Microsoft.AspNetCore.Mvc;
    using System.Net.Http;
    using System.Text;
    using System.Text.Json;

    [ApiController]
    [Route("api/[controller]")]
    public class EmailController : ControllerBase
    {
        private readonly IHttpClientFactory _httpClientFactory;

        public EmailController(IHttpClientFactory httpClientFactory)
        {
            _httpClientFactory = httpClientFactory;
        }

        [HttpPost("send-with-attachment")]
        public async Task<IActionResult> SendEmailWithAttachment()
        {
            var client = _httpClientFactory.CreateClient();
            client.DefaultRequestHeaders.Add("Authorization", "Bearer <API_KEY>");

            var payload = new
            {
                to = "user@example.com",
                subject = "Your requested document",
                body = "Please find the attached file.",
                attachments = new[]
                {
                    new
                    {
                        filename = "document.pdf",
                        content = "<BASE64_ENCODED_CONTENT>",
                        contentType = "application/pdf"
                    }
                }
            };

            var json = JsonSerializer.Serialize(payload);
            var content = new StringContent(json, Encoding.UTF8, "application/json");

            var response = await client.PostAsync(
                "https://api.thepurplebox.io/v1/send", 
                content
            );

            if (response.IsSuccessStatusCode)
            {
                return Ok(new { message = "Email sent successfully" });
            }

            return BadRequest(new { message = "Failed to send email" });
        }
    }
    ```
  </Tab>

  <Tab label="cURL" title="cURL" icon="terminal">
    ```bash theme={null}
    curl -X POST https://api.thepurplebox.io/v1/send \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer <API_KEY>" \
      -d '{
        "to": "user@example.com",
        "subject": "Your requested document",
        "body": "Please find the attached file.",
        "attachments": [
          {
            "filename": "document.pdf",
            "content": "<BASE64_ENCODED_CONTENT>",
            "contentType": "application/pdf"
          }
        ]
      }'
    ```
  </Tab>
</Tabs>
