Rust Email Library for MailSlurp
MailSlurp is an API for sending and receiving emails from dynamically allocated email addresses. It's designed for developers and QA teams to test applications, process inbound emails, send templated notifications, attachments, and more.
Resources
Install
Use cargo to add the MailSlurp crate to your project:
cargo add mailslurp
Or edit your Cargo.toml:
[dependencies]
mailslurp = "x.x.x"
Then run cargo fetch
.
Other dependencies
The MailSlurp library uses the reqwest
HTTP client and async functions. Add tokio
and reqwest
to your Cargo file:
[dependencies]
tokio = { version = "1.4.0", features = ["full"] }
reqwest = { version = "0.11", features = ["json", "multipart"] }
Configure
The MailSlurp SDK lets you create real email accounts for testing and development.
Set API Key
MailSlurp is free to use but you must have an API Key. Create an account to obtain one:

Import and configure
MailSlurp is under the mailslurp
namespace with apis
and models
modules. Controllers are provided that mimic the endpoints of the REST API.
use mailslurp::apis::configuration;
use mailslurp::apis::inbox_controller_api;
fn main() {
const TIMEOUT: Duration = Duration::from_millis(60_000);
let client: Client = reqwest::ClientBuilder::new()
.timeout(TIMEOUT)
.connect_timeout(TIMEOUT)
.build()?;
let api_key: String = env::var("MAILSLURP_API_KEY")?;
let configuration = configuration::Configuration {
base_path: "https://api.mailslurp.com".to_owned(),
api_key: Some(configuration::ApiKey {
prefix: None,
key: api_key,
}),
client,
user_agent: None,
basic_auth: None,
oauth_access_token: None,
bearer_access_token: None,
};
}
Calling controllers
The MailSlurp SDK is generated from the REST API and some methods take many optional parameters. Fill them with None or implement a Default trait if you require.
fn use_controllers() {
let create_inbox_params = inbox_controller_api::CreateInboxParams{
allow_team_access: None,
description: None,
email_address: None,
expires_at: None,
expires_in: None,
favourite: None,
name: None,
tags: None,
use_domain_pool: Some(true)
};
let inbox = inbox_controller_api::create_inbox(&configuration, create_inbox_params).await.ok().unwrap();
assert!(inbox.email_address.unwrap().contains("@mailslurp"));
}
Common usage
Here are some examples for how to send and receive emails and attachments in Rust using MailSlurp.
Create email accounts
MailSlurp inboxes have an email address and ID. Use the ID for further operations against the inbox.
fn create_inbox() {
let create_inbox_params = inbox_controller_api::CreateInboxParams{
allow_team_access: None,
description: None,
email_address: None,
expires_at: None,
expires_in: None,
favourite: None,
name: None,
tags: None,
use_domain_pool: Some(true)
};
let inbox = inbox_controller_api::create_inbox(&configuration, create_inbox_params).await.ok().unwrap();
}
Send emails
You can send HTML emails in MailSlurp:
fn send_email() {
let send_email_params = SendEmailAndConfirmParams{ inbox_id: inbox.id.unwrap().to_owned(), send_email_options: Some(SendEmailOptions{
to: Some(vec!["test@gmail.com".to_owned()]),
body: Some("<html>Email body</html>".to_owned()),
subject: Some("Test subject".to_owned()),
is_html: Some(true),
attachments: None,
bcc: None,
cc: None,
charset: None,
from: None,
reply_to: None,
send_strategy: None,
template: None,
template_variables: None,
to_contacts: None,
to_group: None
}) };
let sent = inbox_controller_api::send_email_and_confirm(&configuration, send_email_params).await.ok().unwrap();
assert!(sent.subject.unwrap().contains("Test subject"));
}
To send attachments first upload each attachment with the attachment controller and save the returned IDs to a variable. Then pass those IDs to the send method as the attachments
property.
Receive email
You can receive emails right in code and tests using the wait controller.
fn receive_email() {
let wait_for_params = WaitForLatestEmailParams {
inbox_id: inbox.id.to_owned(),
timeout: Some(30_000),
unread_only: Some(true)
};
let email = wait_for_controller_api::wait_for_latest_email(&configuration, wait_for_params).await.ok().unwrap();
assert!(email.body.unwrap().contains("Hi"));
}
Testing async
MailSlurp methods are async. Use tokio-test or another implementation to test with async functions.
#[tokio::test]
async fn my_test() -> color_eyre::Result<()> {
color_eyre::install()?;
let create_inbox_params = inbox_controller_api::CreateInboxParams { };
}
See Rust examples page for more help.
More information
See the official Rust homepage or the getting started guide for more information.