Send emails with Rust
Prerequisites
Section titled “Prerequisites”Before you start, you'll need:
- A Resend API key
- A verified domain
Install
First, create a rust project with cargo and
cdinto it.Bash cargo init resend-rust-example cd resend-rust-exampleNext, add add the Rust Resend SDK as well as Tokio:
Bash cargo add resend-rs cargo add tokio -F macros,rt-multi-threadThe Rust SDK is Async-first so Tokio is needed.
Send email
Rust use resend_rs::types::CreateEmailBaseOptions; use resend_rs::{Resend, Result}; #[tokio::main] async fn main() -> Result<()> { let resend = Resend::new("re_xxxxxxxxx"); let from = "Acme <onboarding@resend.dev>"; let to = ["delivered@resend.dev"]; let subject = "Hello World"; let email = CreateEmailBaseOptions::new(from, to, subject) .with_html("<strong>It works!</strong>"); let _email = resend.emails.send(email).await?; Ok(()) }Reading the API key
Instead of using
Resend::newand hardcoding the API key, theRESEND_API_KEYenvironment variable can be used instead. UseResend::default()in that scenario instead.Another popular option is to use a
.envfile for environment variables. You can use thedotenvycrate for that:Bash cargo add dotenvyRust // main.rs use dotenvy::dotenv; use resend_rs::types::CreateEmailBaseOptions; use resend_rs::{Resend, Result}; #[tokio::main] async fn main() -> Result<()> { let _env = dotenv().unwrap(); let resend = Resend::default(); let from = "Acme <onboarding@resend.dev>"; let to = ["delivered@resend.dev"]; let subject = "Hello World"; let email = CreateEmailBaseOptions::new(from, to, subject) .with_html("<strong>It works!</strong>"); let _email = resend.emails.send(email).await?; Ok(()) }TOML # .env RESEND_API_KEY=re_xxxxxxxxx
Examples
Section titled “Examples”Basic Send
Basic email sending
Attachments
Send emails with file attachments
Templates
Send emails using Resend hosted templates
Scheduling
Schedule emails for future delivery
Audiences
Manage contacts and audiences
Domains
Create and manage sending domains
Inbound Webhooks
Receive and process inbound emails
Double Opt-in
Double opt-in subscription flow
Axum App
Full Axum web framework application