Module lettre::smtp [] [src]

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:

Simple example

This is the most basic example of usage:

use lettre::{SimpleSendableEmail, EmailTransport, EmailAddress, SmtpTransport};

let email = SimpleSendableEmail::new(
                EmailAddress::new("user@localhost".to_string()),
                vec![EmailAddress::new("root@localhost".to_string())],
                "message_id".to_string(),
                "Hello world".to_string(),
            );

// Open a local connection on port 25
let mut mailer =
SmtpTransport::builder_unencrypted_localhost().unwrap().build();
// Send the email
let result = mailer.send(&email);

assert!(result.is_ok());

Complete example

use lettre::smtp::authentication::{Credentials, Mechanism};
use lettre::smtp::SUBMISSION_PORT;
use lettre::{SimpleSendableEmail, EmailTransport, EmailAddress, SmtpTransport};
use lettre::smtp::extension::ClientId;
use lettre::smtp::ConnectionReuseParameters;


let email = SimpleSendableEmail::new(
                EmailAddress::new("user@localhost".to_string()),
                vec![EmailAddress::new("root@localhost".to_string())],
                "message_id".to_string(),
                "Hello world".to_string(),
            );

// Connect to a remote server on a custom port
let mut mailer = SmtpTransport::simple_builder("server.tld".to_string()).unwrap()
    // Set the name sent during EHLO/HELO, default is `localhost`
    .hello_name(ClientId::Domain("my.hostname.tld".to_string()))
    // Add credentials for authentication
    .credentials(Credentials::new("username".to_string(), "password".to_string()))
    // Enable SMTPUTF8 if the server supports it
    .smtp_utf8(true)
    // Configure expected authentication mechanism
    .authentication_mechanism(Mechanism::Plain)
    // Enable connection reuse
    .connection_reuse(ConnectionReuseParameters::ReuseUnlimited).build();

let result_1 = mailer.send(&email);
assert!(result_1.is_ok());

// The second email will use the same connection
let result_2 = mailer.send(&email);
assert!(result_2.is_ok());

// Explicitly close the SMTP transaction as we enabled connection reuse
mailer.close();

Lower level

You can also send commands, here is a simple email transaction without error handling:

use lettre::EmailAddress;
use lettre::smtp::SMTP_PORT;
use lettre::smtp::client::Client;
use lettre::smtp::client::net::NetworkStream;
use lettre::smtp::extension::ClientId;
use lettre::smtp::commands::*;

let mut email_client: Client<NetworkStream> = Client::new();
let _ = email_client.connect(&("localhost", SMTP_PORT), None);
let _ = email_client.smtp_command(EhloCommand::new(ClientId::new("my_hostname".to_string())));
let _ = email_client.smtp_command(
            MailCommand::new(Some(EmailAddress::new("user@example.com".to_string())), vec![])
        );
let _ = email_client.smtp_command(
            RcptCommand::new(EmailAddress::new("user@example.org".to_string()), vec![])
        );
let _ = email_client.smtp_command(DataCommand);
let _ = email_client.message(Box::new("Test email".as_bytes()));
let _ = email_client.smtp_command(QuitCommand);

Modules

authentication

Provides authentication mechanisms

client

SMTP client

commands

SMTP commands

error

Error and result type for SMTP clients

extension

ESMTP features

response

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

util

Utils for string manipulation

Structs

SmtpTransport

Structure that implements the high level SMTP client

SmtpTransportBuilder

Contains client configuration

Enums

ClientSecurity

How to apply TLS to a client connection

ConnectionReuseParameters

Configures connection reuse behavior

Constants

COLON

Colon

CRLF

The line ending for SMTP transactions (carriage return + line feed)

MESSAGE_ENDING

The ending of message content

NUL

NUL unicode character

SMTP_PORT

Default smtp port

SP

The word separator for SMTP transactions

SUBMISSION_PORT

Default submission port