Skip to main content

Prerequisites

To get the most out of this guide, you’ll need to:

Install

First, create a rust project with cargo and cd into it.
cargo init emailr-rust-example
cd emailr-rust-example
Next, add add the Rust Emailr SDK as well as Tokio:
cargo add emailr-rs
cargo add tokio -F macros,rt-multi-thread
The Rust SDK is Async-first so Tokio is needed.

Send email

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

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

  let from = "Acme <[email protected]>";
  let to = ["[email protected]"];
  let subject = "Hello World";

  let email = CreateEmailBaseOptions::new(from, to, subject)
    .with_html("<strong>It works!</strong>");

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

  Ok(())
}

Reading the API key

Instead of using Emailr::new and hardcoding the API key, the EMAILR_API_KEY environment variable can be used instead. The Emailr::default() should be used in that scenario instead.

Reading the API key from a .env file

Another popular option is to use a .env file for environment variables. You can use the dotenvy crate for that:
cargo add dotenvy
// main.rs
use dotenvy::dotenv;
use emailr_rs::types::CreateEmailBaseOptions;
use emailr_rs::{Emailr, Result};

#[tokio::main]
async fn main() -> Result<()> {
  let _env = dotenv().unwrap();

  let emailr = Emailr::default();

  let from = "Acme <[email protected]>";
  let to = ["[email protected]"];
  let subject = "Hello World";

  let email = CreateEmailBaseOptions::new(from, to, subject)
    .with_html("<strong>It works!</strong>");

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

  Ok(())
}
# .env
EMAILR_API_KEY=em_xxxxxxxxx

3. Try it yourself

Rust Examples

See the full source code.