#send-email #email-client #send-receive

mailslurp

Official MailSlurp Email API Client

210 stable releases

14.0.1 Sep 11, 2021
13.1.0 Aug 31, 2021
12.8.4 Jul 28, 2021
11.16666.0 May 16, 2021
8.2.16 Nov 24, 2020

#50 in Email

27 downloads per month

MIT license

1MB
13K SLoC

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:

api key

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() {
    // 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,
    };
}

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() {
    // 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"));
}

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() {
    // 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();
}

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{
        // 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.

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<()> {
    // 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.

More information

See the official Rust homepage or the getting started guide for more information.

Dependencies

~5–16MB
~222K SLoC