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.
Use cargo to add the MailSlurp crate to your project:
bash
cargo add mailslurp
Or edit your Cargo.toml:
toml
[dependencies]
mailslurp = "x.x.x"
Then run cargo fetch
.
The MailSlurp library uses the reqwest
HTTP client and async functions. Add tokio
and reqwest
to your Cargo file:
toml
[dependencies]
tokio = { version = "1.4.0", features = ["full"] }
reqwest = { version = "0.11", features = ["json", "multipart"] }
The MailSlurp SDK lets you create real email accounts for testing and development.
MailSlurp is free to use but you must have an API Key. Create an account to obtain one:
MailSlurp is under the mailslurp
namespace with apis
and models
modules. Controllers are provided that mimic the endpoints of the REST API.
```rust use mailslurp::apis::configuration; use mailslurp::apis::inboxcontrollerapi;
fn main() { let client = reqwest::Client::new(); // read mailslurp api key from environment variable or a string let apikey = env::var("MAILSLURPAPIKEY").ok().unwrap(); // configure mailslurp with base path, api key, and reqwest client let configuration = configuration::Configuration { // required fields basepath: "https://api.mailslurp.com".toowned(), apikey: Some(configuration::ApiKey { prefix: None, key: apikey, }), client, // leave as none useragent: None, basicauth: None, oauthaccesstoken: None, beareraccess_token: None, }; } ```
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.
rust
fn use_controllers() {
// create an 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)
};
// methods are async and return results
let inbox = inbox_controller_api::create_inbox(&configuration, create_inbox_params).await.ok().unwrap();
// entity fields are options but see rest api docs for what will be set
assert!(inbox.email_address.unwrap().contains("@mailslurp"));
}
Here are some examples for how to send and receive emails and attachments in Rust using MailSlurp.
MailSlurp inboxes have an email address and ID. Use the ID for further operations against the inbox.
rust
fn create_inbox() {
// create an 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)
};
// methods are async and return results
let inbox = inbox_controller_api::create_inbox(&configuration, create_inbox_params).await.ok().unwrap();
}
You can send HTML emails in MailSlurp:
rust
fn send_email() {
let send_email_params = SendEmailAndConfirmParams{ inbox_id: inbox.id.unwrap().to_owned(), send_email_options: Some(SendEmailOptions{
// common params
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),
// extras
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.
You can receive emails right in code and tests using the wait controller.
rust
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"));
}
See the official Rust homepage or the getting started guide for more information.