Available 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::{header::ContentType, 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")
.header(ContentType::TEXT_PLAIN)
.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-Type: text/plain; charset=utf-8
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 std::fs;
use lettre::message::{header, Attachment, Body, Message, MultiPart, SinglePart};
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--
Re-exports
pub use dkim::*;
Modules
- Headers widely used in email messages
Structs
SinglePart
builder for attachments- A
Message
orSinglePart
body that has already been encoded. - Represents an email address with an optional name for the sender/recipient.
- Represents a sequence of
Mailbox
instances. - Email message which can be formatted
- A builder for messages
- Multipart variant with parts
- Multipart builder
- Single part
- Creates builder for single part
Enums
- Either a
Vec<u8>
or aString
. - The kind of multipart
Traits
- A trait for something that takes an encoded
Body
.