lettre library for Rust: sending email
\n\n1. What it is and why it's needed
\n\nlettre (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\nWhy 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\nKey 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\n2. Installation
\n\nTo install the lettre library, add it to your project's Cargo.toml file:
[dependencies]\nlettre = "0.11"\n\nFor asynchronous work with tokio (recommended):
\n\n[dependencies]\nlettre = { version = "0.11", features = ["tokio1-rustls-tls", "builder"] }\ntokio = { version = "1", features = ["full"] }\n\nFor synchronous work (without an async runtime):
\n\n[dependencies]\nlettre = { version = "0.11", features = ["rustls-tls", "builder"] }\n\nAfter adding the dependencies, run the command:
\n\ncargo build\n\n3. Quick start
\n\nMinimal working example of sending an email via Gmail SMTP server:
\n\nuse 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\n4. Main methods and structures
\n\n4.1. Message::builder()
\nSignature: pub fn builder() -> MessageBuilder
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\nuse 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