lettre for Rust: complete guide with examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

lettre library for Rust: sending email

\n\n

1. What it is and why it's needed

\n\n

lettre (from French "letter") is a powerful, asynchronous, and type-safe library for sending emails in the Rust language. It provides a complete set of tools for creating and sending email messages, supporting both simple text emails and complex HTML emails with attachments. The library is written in pure Rust without external system-level dependencies, making it an ideal choice for projects where portability and security are important.

\n\n

Why is lettre needed? In modern web development, sending email remains a critical function: user registration, password reset, notifications, newsletters. Rust, being a systems language, traditionally lacked convenient tools for working with email. lettre fills this gap by offering an elegant API, full support for SMTP, MIME, and SSL/TLS standards, as well as integration with popular asynchronous runtimes (tokio, async-std).

\n\n

Key features of the library include: support for SMTP, SMTPS, and SMTP via STARTTLS; creation of multipart MIME messages with attachments; working with HTML and text parts; support for asynchronous sending; automatic encoding of attachments and headers; and full type safety at compile time, which prevents many errors typical of dynamic languages.

\n\n

2. Installation

\n\n

To install the lettre library, add it to your project's Cargo.toml file:

\n\n
[dependencies]\nlettre = "0.11"
\n\n

For asynchronous work with tokio (recommended):

\n\n
[dependencies]\nlettre = { version = "0.11", features = ["tokio1-rustls-tls", "builder"] }\ntokio = { version = "1", features = ["full"] }
\n\n

For synchronous work (without an async runtime):

\n\n
[dependencies]\nlettre = { version = "0.11", features = ["rustls-tls", "builder"] }
\n\n

After adding the dependencies, run the command:

\n\n
cargo build
\n\n

3. Quick start

\n\n

Minimal working example of sending an email via Gmail SMTP server:

\n\n
use lettre::message::header::ContentType;\nuse lettre::transport::smtp::authentication::Credentials;\nuse lettre::{Message, SmtpTransport, Transport};\n\nfn main() -> Result<(), Box<dyn std::error::Error>> {\n    // Create the email\n    let email = Message::builder()\n        .from("Sender <your_email@gmail.com>".parse()?)\n        .to("Recipient <recipient@example.com>".parse()?)\n        .subject("Hello from Rust!")\n        .header(ContentType::TEXT_PLAIN)\n        .body("This is a test email sent using lettre.".to_string())?;\n\n    // Configure SMTP transport\n    let creds = Credentials::new(\n        "your_email@gmail.com".to_string(),\n        "your_app_password".to_string() // Use an app password\n    );\n\n    let mailer = SmtpTransport::relay("smtp.gmail.com")?\n        .credentials(creds)\n        .build();\n\n    // Send\n    match mailer.send(&email) {\n        Ok(_) => println!("Email sent successfully!"),\n        Err(e) => eprintln!("Send error: {:?}", e),\n    }\n\n    Ok(())\n}
\n\n

4. Main methods and structures

\n\n

4.1. Message::builder()

\n

Signature: pub fn builder() -> MessageBuilder

\n

Description: Creates a new builder for constructing an email message. The builder allows you to set all message parameters using a method chain: sender, recipients, subject, content, and headers.

\n\n
use lettre::Message;\n\nlet email = Message::builder()\n    .from("user@example.com".parse()?)\n    .to("recipient@example.com".parse()?)\n    .subject("Test")\n    .body("Pr                    

Recommendations