logo
Available on crate feature smtp-transport only.
Expand description

The SMTP transport sends emails using the SMTP protocol.

This SMTP client follows RFC 5321, and is designed to efficiently send emails from an application to a relay email server, as it relies as much as possible on the relay server for sanity and RFC compliance checks.

It implements the following extensions:

SMTP Transport

This transport uses the SMTP protocol to send emails over the network (locally or remotely).

It is designed to be:

  • Secured: connections are encrypted by default
  • Modern: unicode support for email contents and sender/recipient addresses when compatible
  • Fast: supports connection reuse and pooling

This client is designed to send emails to a relay server, and should not be used to send emails directly to the destination server.

The relay server can be the local email server, a specific host or a third-party service.

Simple example

This is the most basic example of usage:

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!"))?;

// Create TLS transport on port 465
let sender = SmtpTransport::relay("smtp.example.com")?.build();
// Send the email via remote relay
let result = sender.send(&email);
assert!(result.is_ok());
Authentication

Example with authentication and connection pool:

use lettre::{
    transport::smtp::{
        authentication::{Credentials, Mechanism},
        PoolConfig,
    },
    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!"))?;

// Create TLS transport on port 587 with STARTTLS
let sender = SmtpTransport::starttls_relay("smtp.example.com")?
    // Add credentials for authentication
    .credentials(Credentials::new(
        "username".to_string(),
        "password".to_string(),
    ))
    // Configure expected authentication mechanism
    .authentication(vec![Mechanism::Plain])
    // Connection pool settings
    .pool_config(PoolConfig::new().max_size(20))
    .build();

// Send the email via remote relay
let result = sender.send(&email);
assert!(result.is_ok());

You can specify custom TLS settings:

use lettre::{
    transport::smtp::client::{Tls, TlsParameters},
    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!"))?;

// Custom TLS configuration
let tls = TlsParameters::builder("smtp.example.com".to_string())
    .dangerous_accept_invalid_certs(true)
    .build()?;

// Create TLS transport on port 465
let sender = SmtpTransport::relay("smtp.example.com")?
    // Custom TLS configuration
    .tls(Tls::Required(tls))
    .build();

// Send the email via remote relay
let result = sender.send(&email);
assert!(result.is_ok());

Modules

Provides limited SASL authentication mechanisms

SMTP client

SMTP commands

ESMTP features

SMTP response, containing a mandatory return code and an optional text message

Structs

AsyncSmtpTransporttokio1 or async-std1

Asynchronously sends emails using the SMTP protocol

AsyncSmtpTransportBuildertokio1 or async-std1

Contains client configuration. Instances of this struct can be created using functions of AsyncSmtpTransport.

The Errors that may occur when sending an email over SMTP

Configuration for a connection pool

Sends emails using the SMTP protocol

Contains client configuration. Instances of this struct can be created using functions of SmtpTransport.

Constants

Default smtp port

Default submission over TLS port

Default submission port