Module lettre::message[][src]

This is supported on crate feature builder only.
Expand description

Provides a strongly typed way to build emails

Usage

This section demonstrates how to build messages.

Plain body

The easiest way of creating a message, which uses a plain text body.

use lettre::message::Message;

let m = Message::builder()
    .from("NoBody <nobody@domain.tld>".parse()?)
    .reply_to("Yuin <yuin@domain.tld>".parse()?)
    .to("Hei <hei@domain.tld>".parse()?)
    .subject("Happy new year")
    .body(String::from("Be happy!"))?;

Which produces:

Click to expand
From: NoBody <nobody@domain.tld>
Reply-To: Yuin <yuin@domain.tld>
To: Hei <hei@domain.tld>
Subject: Happy new year
Date: Sat, 12 Dec 2020 16:33:19 GMT
Content-Transfer-Encoding: 7bit

Be happy!

The unicode header data is encoded using UTF8-Base64 encoding, when necessary.

The Content-Transfer-Encoding is chosen based on the best encoding available for the given body, between 7bit, quoted-printable and base64.

Plain and HTML body

Uses a MIME body to include both plain text and HTML versions of the body.

use lettre::message::{header, Message, MultiPart, SinglePart};

let m = Message::builder()
    .from("NoBody <nobody@domain.tld>".parse()?)
    .reply_to("Yuin <yuin@domain.tld>".parse()?)
    .to("Hei <hei@domain.tld>".parse()?)
    .subject("Happy new year")
    .multipart(MultiPart::alternative_plain_html(
        String::from("Hello, world! :)"),
        String::from("<p><b>Hello</b>, <i>world</i>! <img src=\"cid:123\"></p>"),
    ))?;

Which produces:

Click to expand
From: NoBody <nobody@domain.tld>
Reply-To: Yuin <yuin@domain.tld>
To: Hei <hei@domain.tld>
Subject: Happy new year
MIME-Version: 1.0
Date: Sat, 12 Dec 2020 16:33:19 GMT
Content-Type: multipart/alternative; boundary="0oVZ2r6AoLAhLlb0gPNSKy6BEqdS2IfwxrcbUuo1"

--0oVZ2r6AoLAhLlb0gPNSKy6BEqdS2IfwxrcbUuo1
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 7bit

Hello, world! :)
--0oVZ2r6AoLAhLlb0gPNSKy6BEqdS2IfwxrcbUuo1
Content-Type: text/html; charset=utf8
Content-Transfer-Encoding: 7bit

<p><b>Hello</b>, <i>world</i>! <img src="cid:123"></p>
--0oVZ2r6AoLAhLlb0gPNSKy6BEqdS2IfwxrcbUuo1--

Complex MIME body

This example shows how to include both plain and HTML versions of the body, attachments and inlined images.

use lettre::message::{header, Attachment, Body, Message, MultiPart, SinglePart};
use std::fs;

let image = fs::read("docs/lettre.png")?;
// this image_body can be cloned and reused between emails.
// since `Body` holds a pre-encoded body, reusing it means avoiding having
// to re-encode the same body for every email (this clearly only applies
// when sending multiple emails with the same attachment).
let image_body = Body::new(image);

let m = Message::builder()
    .from("NoBody <nobody@domain.tld>".parse()?)
    .reply_to("Yuin <yuin@domain.tld>".parse()?)
    .to("Hei <hei@domain.tld>".parse()?)
    .subject("Happy new year")
    .multipart(
        MultiPart::mixed()
            .multipart(
                MultiPart::alternative()
                    .singlepart(SinglePart::plain(String::from("Hello, world! :)")))
                    .multipart(
                        MultiPart::related()
                            .singlepart(SinglePart::html(String::from(
                                "<p><b>Hello</b>, <i>world</i>! <img src=cid:123></p>",
                            )))
                            .singlepart(
                                Attachment::new_inline(String::from("123"))
                                    .body(image_body, "image/png".parse().unwrap()),
                            ),
                    ),
            )
            .singlepart(Attachment::new(String::from("example.rs")).body(
                String::from("fn main() { println!(\"Hello, World!\") }"),
                "text/plain".parse().unwrap(),
            )),
    )?;

Which produces:

Click to expand
From: NoBody <nobody@domain.tld>
Reply-To: Yuin <yuin@domain.tld>
To: Hei <hei@domain.tld>
Subject: Happy new year
MIME-Version: 1.0
Date: Sat, 12 Dec 2020 16:30:45 GMT
Content-Type: multipart/mixed; boundary="0oVZ2r6AoLAhLlb0gPNSKy6BEqdS2IfwxrcbUuo1"

--0oVZ2r6AoLAhLlb0gPNSKy6BEqdS2IfwxrcbUuo1
Content-Type: multipart/alternative; boundary="EyXdAZIgZuyUjAounq4Aj44a6MpJfqCKhm6pE1zk"

--EyXdAZIgZuyUjAounq4Aj44a6MpJfqCKhm6pE1zk
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 7bit

Hello, world! :)
--EyXdAZIgZuyUjAounq4Aj44a6MpJfqCKhm6pE1zk
Content-Type: multipart/related; boundary="eM5Z18WZVOQsqi5GQ71XGAXk6NNvHUA1Xv1FWrXr"

--eM5Z18WZVOQsqi5GQ71XGAXk6NNvHUA1Xv1FWrXr
Content-Type: text/html; charset=utf8
Content-Transfer-Encoding: 7bit

<p><b>Hello</b>, <i>world</i>! <img src=cid:123></p>
--eM5Z18WZVOQsqi5GQ71XGAXk6NNvHUA1Xv1FWrXr
Content-Type: image/png
Content-Disposition: inline
Content-ID: <123>
Content-Transfer-Encoding: base64

PHNtaWxlLXJhdy1pbWFnZS1kYXRhPg==
--eM5Z18WZVOQsqi5GQ71XGAXk6NNvHUA1Xv1FWrXr--
--EyXdAZIgZuyUjAounq4Aj44a6MpJfqCKhm6pE1zk--
--0oVZ2r6AoLAhLlb0gPNSKy6BEqdS2IfwxrcbUuo1
Content-Type: text/plain; charset=utf8
Content-Disposition: attachment; filename="example.rs"
Content-Transfer-Encoding: 7bit

fn main() { println!("Hello, World!") }
--0oVZ2r6AoLAhLlb0gPNSKy6BEqdS2IfwxrcbUuo1--

Modules

header

Headers widely used in email messages

Structs

Attachment

SinglePart builder for attachments

Body

A Message or SinglePart body that has already been encoded.

Mailbox

Represents an email address with an optional name for the sender/recipient.

Mailboxes

Represents a sequence of Mailbox instances.

Message

Email message which can be formatted

MessageBuilder

A builder for messages

MultiPart

Multipart variant with parts

MultiPartBuilder

Multipart builder

SinglePart

Single part

SinglePartBuilder

Creates builder for single part

Enums

MaybeString

Either a Vec<u8> or a String.

MultiPartKind

The kind of multipart

Traits

IntoBody

A trait for something that takes an encoded Body.