mailtrap

Crates.iomailtrap
lib.rsmailtrap
version0.1.3
created_at2026-01-24 17:37:27.498623+00
updated_at2026-01-25 17:01:10.200822+00
descriptionAn unofficial library for interacting with the Mailtrap API
homepagehttps://github.com/m3talsmith/mailtrap
repositoryhttps://github.com/m3talsmith/mailtrap
max_upload_size
id2067046
size133,588
Michael Christenson II (m3talsmith)

documentation

https://docs.rs/mailtrap

README

Mailtrap

An unofficial Rust library for interacting with the Mailtrap API. This library allows you to create and send emails programmatically using Mailtrap's sending API.

Installation

Add this to your Cargo.toml:

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

Usage

Here's a simple example of how to send an email:

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>
    // You need to provide the API endpoint and your API token.
    // The API endpoint is usually "https://send.api.mailtrap.io/" for production.
    let res = email.send(
        None,
        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 Usage

You can also send emails with HTML content, attachments, and custom headers.

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

#[tokio::main]
async fn main() {
    // Create an attachment
    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 Name\" <sender@example.com>")
        .to("recipient@example.com")
        .cc("cc@example.com")
        .bcc("bcc@example.com")
        .reply_to("reply@example.com")
        .subject("Advanced Email Test")
        .text("Plain text content")
        .html("<h1>HTML Content</h1><p>This is an email with HTML.</p>")
        .attachments(vec![attachment])
        .header("X-Custom-Header", "Custom Value")
        .category("integration-test");

    let res = email.send(
        None,
        Some("YOUR_API_TOKEN"),
        None
    ).await;
    
    // handle response...
}

Batch Sending and Templates

You can send emails to multiple recipients in a batch, and use templates with variable substitution.

use mailtrap::{BatchEmail, BatchEmailRequest};

#[tokio::main]
async fn main() {
    // Create a request for a specific recipient
    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");

    // Create the batch email
    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(success) => {
            if success {
                println!("Batch email sent successfully!");
            } else {
                println!("Failed to send batch email.");
            }
        }
        Err(e) => eprintln!("Error: {}", e),
    }
}

Features

  • Simple Builder API: Create emails with a fluent interface.
  • Multiple Content Types: Support for both plain text and HTML content.
  • Attachments: Easy support for file attachments with customizable content type and disposition.
  • Custom Headers: Add any custom headers to your emails.
  • Batch Sending: Send emails to multiple recipients in a single API call.
  • Templates: Support for Mailtrap templates with variables.
  • Categories: Tag your emails with categories for analytics.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Commit count: 9

cargo fmt