Expand description
§Transports for sending emails
This module contains Transport
s for sending emails. A Transport
implements a high-level API
for sending emails. It automatically manages the underlying resources and doesn’t require any
specific knowledge of email protocols in order to be used.
§Getting started
Sending emails from your programs requires using an email relay, as client libraries are not designed to handle email delivery by themselves. Depending on your infrastructure, your relay could be:
- a service from your Cloud or hosting provider
- an email server (MTA for Mail Transfer Agent, like Postfix or Exchange), running either locally on your servers or accessible over the network
- a dedicated external service, like Mailchimp, Mailgun, etc.
In most cases, the best option is to:
- Use the
SMTP
transport, with therelay
builder (or one of its async counterparts) with your server’s hostname. They provide modern and secure defaults. - Use the
credentials
method of the builder to pass your credentials.
These should be enough to safely cover most use cases.
§Available transports
The following transports are available:
Module | Protocol | Sync API | Async API | Description |
---|---|---|---|---|
smtp | SMTP | SmtpTransport | AsyncSmtpTransport | Uses the SMTP protocol to send emails to a relay server |
[sendmail ] | Sendmail | SendmailTransport | AsyncSendmailTransport | Uses the sendmail command to send emails |
file | File | FileTransport | AsyncFileTransport | Saves the email as an .eml file |
stub | Debug | StubTransport | StubTransport | Drops the email - Useful for debugging |
§Building an email
Emails can either be built though Message
, which is a typed API for constructing emails
(find out more about it by going over the message
module),
or via external means.
Message
s can be sent via Transport::send
or [AsyncTransport::send
], while messages
built without lettre’s message
APIs can be sent via Transport::send_raw
or [AsyncTransport::send_raw
].
§Brief example
This example shows how to build an email and send it via an SMTP relay server. It is in no way a complete example, but it shows how to get started with lettre. More examples can be found by looking at the specific modules, linked in the Module column of the table above.
use lettre::{transport::smtp::authentication::Credentials, Message, SmtpTransport, Transport};
let email = 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!"))?;
let creds = Credentials::new("smtp_username".to_owned(), "smtp_password".to_owned());
// Open a remote connection to the SMTP relay server
let mailer = SmtpTransport::relay("smtp.gmail.com")?
.credentials(creds)
.build();
// Send the email
match mailer.send(&email) {
Ok(_) => println!("Email sent successfully!"),
Err(e) => panic!("Could not send email: {:?}", e),
}
Modules§
- The SMTP transport sends emails using the SMTP protocol.
- The stub transport logs message envelopes as well as contents. It can be useful for testing purposes.
Traits§
- Blocking Transport method for emails