Sending Email in Rust: Powerful Tools for Working with Mail
Rust is a systems programming language increasingly used for building reliable and high-performance web services. One common task is sending emails. In this article, we will thoroughly examine two key libraries in the Rust ecosystem for working with email: lettre (sending) and mailparse (parsing). You will learn how to configure an SMTP client, send an email with attachments, and process incoming mail.
The lettre Library: Sending Emails via SMTP
lettre is a modern, asynchronous and synchronous library for sending email via SMTP, SMTP-SSL, and STARTTLS. It supports:
- Sending text and HTML emails
- Attachments (images, documents, archives)
- Authentication (LOGIN, PLAIN, XOAUTH2)
- DKIM signing and TLS encryption
- Working with async/await (tokio, async-std)
To get started, add the dependency to your Cargo.toml:
[dependencies]lettre = { version = "0.11", features = ["builder", "smtp-transport", "tokio1-rustls-tls"] }tokio = { version = "1", features = ["full"] }Basic Example: Sending a Text Email
use lettre::transport::smtp::authentication::Credentials;use lettre::{Message, SmtpTransport, Transport};
#[tokio::main]async fn main() -> Result<(), Box<dyn std::error::Error>> { // Build the email let email = Message::builder() .from("Sender <sender@example.com>".parse()?) .to("Recipient <recipient@example.com>".parse()?) .subject("Hello from Rust!") .body("This is a test email sent using the lettre library.".to_string())?;
// SMTP configuration (Gmail, Yandex, Mail.ru, etc.) let creds = Credentials::new("sender@example.com".to_string(), "your_password".to_string()); let mailer = SmtpTransport::relay("smtp.gmail.com")? .credentials(creds) .build();
// Sending match mailer.send(&email) { Ok(_) => println!("Email sent successfully!"), Err(e) => eprintln!("Error sending: {}", e), } Ok(())}Sending an HTML Email with Attachments
To send an email with HTML markup and a file, use MultiPart:
use lettre::message::{header, MultiPart, SinglePart};use lettre::{Message, SmtpTransport, Transport};use std::fs;
let file_content = fs::read("document.pdf")?;let attachment = SinglePart::builder() .header(header::ContentType::parse("application/pdf")?) .header(header::ContentDisposition::attachment("document.pdf")) .body(file_content);
let html_part = SinglePart::builder() .header(header::ContentType::TEXT_HTML) .body(String::from("<h1>Hello!</h1><p>This is an HTML email with an attachment.</p>"));
let email = Message::builder() .from("sender@example.com".parse()?) .to("recipient@example.com".parse()?) .subject("HTML + attachment") .multipart( MultiPart::mixed() .multipart(MultiPart::alternative().singlepart(html_part)) .singlepart(attachment), )?;The mailparse Library: Parsing Incoming Emails
mailparse is a lightweight library for parsing email messages in MIME format. It is useful if you are processing mail via IMAP/POP3 or reading .eml files. Key features:
- Extracting headers (From, To, Subject, Date)
- Retrieving the email body (text/HTML)
- Extracting attachments with MIME types
- Decoding Base64 and Quoted-Printable
Example: Reading a .eml File
use mailparse::*;
fn main() { let eml_content = std::fs::read("email.eml")