| Crates.io | mailtrap |
| lib.rs | mailtrap |
| version | 0.1.3 |
| created_at | 2026-01-24 17:37:27.498623+00 |
| updated_at | 2026-01-25 17:01:10.200822+00 |
| description | An unofficial library for interacting with the Mailtrap API |
| homepage | https://github.com/m3talsmith/mailtrap |
| repository | https://github.com/m3talsmith/mailtrap |
| max_upload_size | |
| id | 2067046 |
| size | 133,588 |
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.
Add this to your Cargo.toml:
[dependencies]
mailtrap = "0.1.3"
tokio = { version = "1", features = ["full"] }
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),
}
}
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...
}
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),
}
}
This project is licensed under the MIT License - see the LICENSE file for details.