Module lettre::transport::smtp[][src]

This is supported 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, Transport, SmtpTransport};

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::{Message, Transport, SmtpTransport, transport::smtp::{PoolConfig, authentication::{Credentials, Mechanism}}};

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::{Message, Transport, SmtpTransport, transport::smtp::client::{TlsParameters, Tls}};

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

authentication

Provides limited SASL authentication mechanisms

client

SMTP client

commands

SMTP commands

extension

ESMTP features

response

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

util

Utils for string manipulation

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.

Error

The Errors that may occur when sending an email over SMTP

PoolConfigr2d2

Configuration for a connection pool

SmtpTransport

Sends emails using the SMTP protocol

SmtpTransportBuilder

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

Constants

DEFAULT_TIMEOUT

Default timeout

SMTP_PORT

Default smtp port

SUBMISSIONS_PORT

Default submission over TLS port

SUBMISSION_PORT

Default submission port