# Send email from Rust

`cargo add mailset`, then `mailset.send(message).await?`. The client is async on Tokio, and every command’s input and answer is a struct or an enum that cargo clippy checks.

## Install the Rust SDK

cargo, or the line for Cargo.toml. Rust 1.88 and later. It depends on base64, getrandom, hmac, p256, reqwest, serde, serde_json, sha2 and tokio.

cargo:

```bash
cargo add mailset
```

Cargo.toml:

```toml
[dependencies]
mailset = "0.7.0"
```

## Send your first email from Rust

`Client::from_env()?` reads `MAILSET_KEY`. `Message::builder()` takes each field, `.deliver_by("10m")` included, and `send(message).await?` answers the receipt, `sent.id` and `sent.status`. A refusal comes back as a `mailset::Error`, which `?` hands to your caller.

```rust
use mailset::{Client, Message};

// Reads MAILSET_KEY from the environment.
let mailset = Client::from_env()?;

let message = Message::builder()
    .from("Acme <hello@example.com>")
    .to("ana@example.org")
    .subject("Your Acme code")
    .text("Your code is 123456.")
    .html("<p>Your code is <b>123456</b>.</p>")
    .deliver_by("10m");
let sent = mailset.send(message).await?;

println!("{} {:?}", sent.id, sent.status);
```

## Delivery events in Axum

`mailset_hook` takes the body as `Bytes`, because a `Json` extractor would parse it first and the signature covers the bytes as sent. `webhooks::verify` checks them against the `HeaderMap`; anything but `Ok` answers 401. The handler matches `Event::Bounced` for hard bounces and returns 204 once the batch is kept.

```rust
use axum::body::Bytes;
use axum::http::{HeaderMap, StatusCode};
use mailset::webhooks::{self, Event};

// The signature covers the body exactly as it came:
// take it as `Bytes`. A `Json` extractor would parse
// it first and lose the bytes it was made over.
pub async fn mailset_hook(
    headers: HeaderMap,
    body: Bytes,
) -> StatusCode {
    let secret = std::env::var("MAILSET_HOOK_SECRET");
    let secret = secret.unwrap_or_default();
    let verified =
        webhooks::verify(&body, &headers, &secret);
    let Ok(events) = verified else {
        return StatusCode::UNAUTHORIZED;
    };
    for event in events {
        let Event::Bounced(bounced) = event else {
            continue;
        };
        if bounced.bounce.is_hard() {
            let to = &bounced.recipient;
            println!("Stop writing to {to}");
        }
    }
    // 2xx once the events are kept.
    // Until then Mailset posts the batch again.
    StatusCode::NO_CONTENT
}
```

In the SDK’s tests: compiled against Axum, and run through its router.

## Send from your own domain

Your product sends as your own domain either way. What changes is where your team’s mail lives.

- **Sending only.** Your team’s mail stays at Google, Microsoft or wherever it is now. Mailset asks for DKIM, a bounce host and DMARC, and leaves your MX alone. [Send from a domain whose mail stays elsewhere](https://mailset.app/docs/recipes/sending-only)
- **Mailboxes too.** Your team’s mail moves to Mailset, with a real mailbox for every address on the domain and your product’s email on the same bill. [Set up a domain](https://mailset.app/guide/onboarding)

```bash
mailset domains add example.com --sending
mailset domains apply example.com --sending
mailset domains check example.com
mailset keys create --send --domain example.com --from hello@example.com --name "Acme API"
```

## What the Rust SDK’s build checks

Each line comes from the SDK’s own build, which fails when one stops being true.

- **Rust 1.88+**: The oldest its manifest allows (its manifest)
- **Typed throughout**: Structs and enums, checked by clippy (a test)
- **Every public command**: 238 commands, each a typed method (the catalog)

## Where to go from here

- [The Rust SDK](https://mailset.app/docs/sdk/rust): Every task, from the client’s options to errors and retries.
- [Webhooks](https://mailset.app/docs/webhooks): The events Mailset posts about your product’s email, and how to check a request’s signature.
- [Give sign-in codes a deadline](https://mailset.app/docs/recipes/deadlines): Use deliverBy so a code that would arrive late is never sent.
- [Retry without sending twice](https://mailset.app/docs/recipes/retries): Make a send safe to repeat with an idempotency key or a Message-ID.
- [Handle bounces](https://mailset.app/docs/recipes/bounces): Tell a dead address from a full inbox, and keep your list clean.
- [Send many messages at once](https://mailset.app/docs/recipes/batches): Up to 100 messages a request, each with its own answer.

## Product email on every plan

Both include 10,000 emails a month, your product’s and your team’s together. Past that it’s $0.40 per 1,000 with pay as you go. Every plan and price: https://mailset.app/pricing

## Sending from Rust, asked and answered

### Is there a blocking client?

No. Every call is async on Tokio, so run it from an async function: an Axum handler, or a `main` under `#[tokio::main]`.

### Do I need lettre?

No. lettre speaks SMTP, and Mailset takes your product’s email over HTTPS, which the crate calls through reqwest.

### Should I clone the client?

Freely. Clones share one connection pool, so a clone per handler or task costs nothing new.

### Does it link OpenSSL?

No. reqwest is built with rustls here, so the crate needs no system TLS library.

## Try it on your own domain

7 days free, with everything on. Make a key, run the example above and watch the receipt come back. Start at https://mailset.app/start

---

© 2026 Intenseloop, Inc. Mailset’s site, docs and guides are copyrighted. You may read them and use their code to build with Mailset. Don’t republish them or use them to build a copy of Mailset. Terms: https://mailset.app/terms#content