Crates.io | mailslurp |
lib.rs | mailslurp |
version | 14.0.1 |
source | src |
created_at | 2020-11-17 22:43:09.636978 |
updated_at | 2021-09-11 05:41:50.487666 |
description | Official MailSlurp Email API Client |
homepage | |
repository | |
max_upload_size | |
id | 313433 |
size | 1,162,536 |
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:
cargo add mailslurp
Or edit your Cargo.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:
[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.
use mailslurp::apis::configuration;
use mailslurp::apis::inbox_controller_api;
fn main() {
// allow a long timeout so you can wait for emails to arrive
const TIMEOUT: Duration = Duration::from_millis(60_000);
let client: Client = reqwest::ClientBuilder::new()
.timeout(TIMEOUT)
.connect_timeout(TIMEOUT)
.build()?;
// read mailslurp api key from environment variable or a string
let api_key: String = env::var("MAILSLURP_API_KEY")?;
// configure mailslurp with base path, api key, and reqwest client
let configuration = configuration::Configuration {
// required fields
base_path: "https://api.mailslurp.com".to_owned(),
api_key: Some(configuration::ApiKey {
prefix: None,
key: api_key,
}),
client,
// leave as none
user_agent: None,
basic_auth: None,
oauth_access_token: None,
bearer_access_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.
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.
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:
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.
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"));
}
MailSlurp methods are async. Use tokio-test or another implementation to test with async functions.
#[tokio::test]
async fn my_test() -> color_eyre::Result<()> {
// use color-eyre for better result reports
color_eyre::install()?;
// create an inbox
let create_inbox_params = inbox_controller_api::CreateInboxParams { /* etc */ };
}
See Rust examples page for more help.
See the official Rust homepage or the getting started guide for more information.