Skip to main content

Crate mailtrap

Crate mailtrap 

Source
Expand description

§Mailtrap

mailtrap is an unofficial Rust library for interacting with the Mailtrap API. It allows you to create and send emails programmatically, manage accounts, and handle access permissions.

§Usage

Add this to your Cargo.toml:

[dependencies]
mailtrap = "0.3.1"
tokio = { version = "1", features = ["full"] }

§Examples

§Basic Email Sending

use mailtrap::Email;

#[tokio::main]
async fn main() {
    let email = Email::new()
        .from("sender@example.com")
        .to("recipient@example.com")
        .subject("Hello from Rust")
        .text("This is a test email sent from Rust using the Mailtrap API.")
        .category("test");

    // send returns a Result<bool, Error>
    let res = email.send(
        None, // Use default API URL
        Some("YOUR_API_TOKEN"),
        None
    ).await;

    match res {
        Ok(success) => {
            if success {
                println!("Email sent successfully!");
            } else {
                println!("Failed to send email.");
            }
        }
        Err(e) => eprintln!("Error: {}", e),
    }
}

§Advanced Email (Attachments, HTML, Headers)

use mailtrap::{Email, Attachment, ContentType, Disposition};

#[tokio::main]
async fn main() {
    let attachment = Attachment::new()
        .content("Attachment content".as_bytes().to_vec())
        .filename("test.txt")
        .content_type(ContentType::Plain)
        .disposition(Disposition::Attachment);

    let email = Email::new()
        .from("sender@example.com")
        .to("recipient@example.com")
        .subject("Advanced Email Test")
        .text("Plain text content")
        .html("<h1>HTML Content</h1>")
        .attachments(vec![attachment])
        .header("X-Custom-Header", "Custom Value");

    let res = email.send(None, Some("YOUR_API_TOKEN"), None).await;
     
    match res {
        Ok(_) => println!("Advanced email sent!"),
        Err(e) => eprintln!("Error: {}", e),
    }
}

§Batch Sending and Templates

use mailtrap::{BatchEmail, BatchEmailRequest};

#[tokio::main]
async fn main() {
    let request1 = BatchEmailRequest::new()
        .to("recipient1@example.com")
        .template_uuid("YOUR_TEMPLATE_UUID")
        .template_variable("user_name", "Alice");

    let request2 = BatchEmailRequest::new()
        .to("recipient2@example.com")
        .template_uuid("YOUR_TEMPLATE_UUID")
        .template_variable("user_name", "Bob");

    let batch_email = BatchEmail::new()
        .from("sender@example.com")
        .template_uuid("YOUR_TEMPLATE_UUID")
        .request(request1)
        .request(request2);

    let res = batch_email.send(None, Some("YOUR_API_TOKEN"), None).await;
     
    match res {
        Ok(_) => println!("Batch email sent!"),
        Err(e) => eprintln!("Error: {}", e),
    }
}

§Account Access

use mailtrap::Access;

#[tokio::main]
async fn main() {
    let access = Access::new();
    let account_id = "YOUR_ACCOUNT_ID";

    // List access permissions
    let res = access.list(
        None,
        Some("YOUR_API_TOKEN"),
        None,
        account_id,
        None, None, None
    ).await;
     
    match res {
        Ok(records) => println!("Found {} access records", records.len()),
        Err(e) => eprintln!("Error: {}", e),
    }
}

§Accounts

use mailtrap::Accounts;

#[tokio::main]
async fn main() {
    let accounts_client = Accounts::new();

    // List available accounts
    let res = accounts_client.list(
        None,
        Some("YOUR_API_TOKEN"),
        None
    ).await;

    match res {
        Ok(accounts) => {
            for account in accounts {
                println!("Account: {}", account.name);
            }
        }
        Err(e) => eprintln!("Error: {}", e),
    }
}

Re-exports§

pub use email::*;
pub use access::*;
pub use accounts::*;

Modules§

access
accounts
email