Module lettre::transport [−][src]
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.
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; use lettre::{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_string(), "smtp_password".to_string()); // 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
file | file-transport The file transport writes the emails to the given directory. The name of the file will be
|
sendmail | sendmail-transport The sendmail transport sends the email using the local |
smtp | smtp-transport The SMTP transport sends emails using the SMTP protocol. |
stub | The stub transport only logs message envelope and drops the content. It can be useful for testing purposes. |
Traits
AsyncTransport | tokio02 or tokio1 or async-std1 Async Transport method for emails |
Transport | Blocking Transport method for emails |