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

# Rust

> Official JetEmail Rust SDK for transactional email

# Rust SDK

The **[jetemail](https://crates.io/crates/jetemail)** crate is the official Rust SDK for JetEmail transactional email (MIT license, async with **Tokio**).

Create a **transactional** API key in the dashboard: **Outbound** → **Keys** → Add API key. Use that key with the SDK.

## Installation

Add to your `Cargo.toml`:

```toml theme={null}
[dependencies]
jetemail = "0.1"
tokio = { version = "1", features = ["full"] }
```

Crate on crates.io: [jetemail](https://crates.io/crates/jetemail).

## Quick start

```rust theme={null}
use jetemail::JetEmail;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = JetEmail::new("your-transactional-api-key");

    let result = client.email().send(
        jetemail::SendEmailOptions::builder()
            .from("Your App <hello@yourdomain.com>")
            .to("user@example.com")
            .subject("Welcome!")
            .html("<h1>Welcome!</h1>")
            .build(),
    ).await?;

    println!("{}", result.id);
    Ok(())
}
```

Use a `from` address on a **verified domain** (see [Getting started](/transactional-email/getting-started)).

## Constructor

```rust theme={null}
// API key string
let client = JetEmail::new("your-transactional-api-key");

// Or with options
let client = JetEmail::builder()
    .api_key("your-transactional-api-key")
    .base_url("https://api.jetemail.com") // optional
    .build();
```

## `client.email().send(options)`

Send a single email.

| Field                   | Required | Description                              |
| ----------------------- | -------- | ---------------------------------------- |
| `from`                  | ✓        | Sender, e.g. `"Name <email@domain.com>"` |
| `to`                    | ✓        | Recipient(s), string or vec (max 50)     |
| `subject`               | ✓        | Subject line                             |
| `html`                  | \*       | HTML body (need `html` and/or `text`)    |
| `text`                  | \*       | Plain text (need `html` and/or `text`)   |
| `cc`, `bcc`, `reply_to` |          | Recipients (max 50 each)                 |
| `headers`               |          | Custom headers HashMap                   |
| `attachments`           |          | Attachments (max 40MB total)             |

Response shape: `SendEmailResponse { id: String, response: String }`.

## `client.batch().send(emails)`

Send up to **100** emails in one request. Returns `summary` (`total`, `successful`, `failed`) and `results` per message.

```rust theme={null}
let result = client.batch().send(vec![
    SendEmailOptions::builder()
        .from("you@example.com")
        .to("alice@example.com")
        .subject("Hello Alice")
        .html("<p>Hi Alice!</p>")
        .build(),
    SendEmailOptions::builder()
        .from("you@example.com")
        .to("bob@example.com")
        .subject("Hello Bob")
        .html("<p>Hi Bob!</p>")
        .build(),
]).await?;
```

## Attachments

```rust theme={null}
use jetemail::Attachment;

let result = client.email().send(
    SendEmailOptions::builder()
        .from("you@example.com")
        .to("recipient@example.com")
        .subject("Invoice")
        .html("<p>Please find your invoice attached.</p>")
        .attachments(vec![
            Attachment::from_path("/path/to/invoice.pdf").await?,
            Attachment::from_content(b"Hello World", "hello.txt"),
        ])
        .build(),
).await?;
```

## Error handling

```rust theme={null}
use jetemail::JetEmailError;

match client.email().send(options).await {
    Ok(result) => println!("Sent: {}", result.id),
    Err(JetEmailError::Api { status_code, message, response }) => {
        eprintln!("{}: {}", status_code, message);
    }
    Err(e) => eprintln!("Error: {}", e),
}
```

Common status codes: **400** invalid request, **401** bad/missing API key, **500** server error.

***

For additional examples and the full option list, see the **[crates.io page](https://crates.io/crates/jetemail)**. REST details: [API reference](/api-reference/introduction).
