Module lettre::message [−][src]
builder
only.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, Part, 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() .singlepart( SinglePart::builder() .header(header::ContentType("text/plain; charset=utf8".parse()?)) .body(String::from("Hello, world! :)")), ) .singlepart( SinglePart::builder() .header(header::ContentType("text/html; charset=utf8".parse()?)) .body(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, Body, Message, MultiPart, Part, 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::builder() .header(header::ContentType("text/plain; charset=utf8".parse()?)) .body(String::from("Hello, world! :)")), ) .multipart( MultiPart::related() .singlepart( SinglePart::builder() .header(header::ContentType( "text/html; charset=utf8".parse()?, )) .body(String::from( "<p><b>Hello</b>, <i>world</i>! <img src=cid:123></p>", )), ) .singlepart( SinglePart::builder() .header(header::ContentType("image/png".parse()?)) .header(header::ContentDisposition { disposition: header::DispositionType::Inline, parameters: vec![], }) .header(header::ContentId("<123>".into())) .body(image_body), ), ), ) .singlepart( SinglePart::builder() .header(header::ContentType("text/plain; charset=utf8".parse()?)) .header(header::ContentDisposition { disposition: header::DispositionType::Attachment, parameters: vec![header::DispositionParam::Filename( header::Charset::Ext("utf-8".into()), None, "example.rs".as_bytes().into(), )], }) .body(String::from("fn main() { println!(\"Hello, World!\") }")), ), )?;
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 mime; |
Modules
header | Headers widely used in email messages |
Structs
Body | A |
Mailbox | Represents an email address with an optional name for the sender/recipient. |
Mailboxes | Represents a sequence of |
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 |
MultiPartKind | The kind of multipart |
Part | MIME part variants |
Traits
IntoBody | A trait for something that takes an encoded |
Type Definitions
Parts | Parts of multipart body |