Creating messages

This section explains how to create emails.

Simple example

The email part builds email messages. For now, it does not support attachments. An email is built using an EmailBuilder. The simplest email could be:

extern crate lettre_email;

use lettre_email::EmailBuilder;

fn main() {
    // Create an email
    let email = EmailBuilder::new()
        // Addresses can be specified by the tuple (email, alias)
        .to(("", "Firstname Lastname"))
        // ... or by an address only
        .subject("Hi, Hello world")
        .text("Hello world.")


When the build method is called, the EmailBuilder will add the missing headers (like Message-ID or Date) and check for missing necessary ones (like From or To). It will then generate an Email that can be sent.

The text() method will create a plain text email, while the html() method will create an HTML email. You can use the alternative() method to provide both versions, using plain text as fallback for the HTML version.

Complete example

Below is a more complete example, not using method chaining:

extern crate lettre_email;

use lettre_email::EmailBuilder;

fn main() {
    let mut builder = EmailBuilder::new();
    builder.add_to(("", "Alias name"));
    builder.add_cc(("", "Alias name"));
    builder.set_subject("Hello world");
    builder.set_alternative("<h2>Hi, Hello world.</h2>", "Hi, Hello world.");
    builder.add_header(("X-Custom-Header", "my header"));

    let email =;

See the EmailBuilder documentation for a complete list of methods.

results matching ""

    No results matching ""