18 unstable releases (3 breaking)

Uses old Rust 2015

0.3.0 Oct 12, 2015
0.1.2 Aug 2, 2015
0.1.1 Jul 22, 2015
0.0.9 Mar 20, 2015
0.0.2 Nov 21, 2014

#5 in #mailer

Download history 36/week @ 2021-03-31 24/week @ 2021-04-07 56/week @ 2021-04-14 19/week @ 2021-04-28 1/week @ 2021-05-05 21/week @ 2021-05-12 3/week @ 2021-05-19 1/week @ 2021-06-02 21/week @ 2021-06-09 3/week @ 2021-06-16 19/week @ 2021-07-14

86 downloads per month

MIT license

73KB
1.5K SLoC

rust-smtp Build Status Coverage Status Crate MIT licensed

This library implements a simple SMTP client. See the documentation for more information.

Install

To use this library, add the following to your Cargo.toml:

[dependencies]
smtp = "0.3"

License

This program is distributed under the terms of the MIT license.

See LICENSE for details.


lib.rs:

Rust SMTP client

This client should tend to follow RFC 5321, but is still a work in progress. It 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:

It will eventually implement the following extensions:

Architecture

This client is divided into three main parts:

  • client: a low level SMTP client providing all SMTP commands
  • sender: a high level SMTP client providing an easy method to send emails
  • email: generates the email to be sent with the sender

Usage

Simple example

This is the most basic example of usage:

use smtp::sender::{Sender, SenderBuilder};
use smtp::email::EmailBuilder;

// Create an email
let email = EmailBuilder::new()
    // Addresses can be specified by the couple (email, alias)
    .to(("user@example.org", "Firstname Lastname"))
    // ... or by an address only
    .from("user@example.com")
    .subject("Hi, Hello world")
    .body("Hello world.")
    .build();

// Open a local connection on port 25
let mut sender = SenderBuilder::localhost().unwrap().build();
// Send the email
let result = sender.send(email);

assert!(result.is_ok());

Complete example

use smtp::sender::{Sender, SenderBuilder};
use smtp::email::EmailBuilder;
use smtp::authentication::Mecanism;
use smtp::SUBMISSION_PORT;

let mut builder = EmailBuilder::new();
builder = builder.to(("user@example.org", "Alias name"));
builder = builder.cc(("user@example.net", "Alias name"));
builder = builder.from("no-reply@example.com");
builder = builder.from("no-reply@example.eu");
builder = builder.sender("no-reply@example.com");
builder = builder.subject("Hello world");
builder = builder.body("Hi, Hello world.");
builder = builder.reply_to("contact@example.com");
builder = builder.add_header(("X-Custom-Header", "my header"));

let email = builder.build();

// Connect to a remote server on a custom port
let mut sender = SenderBuilder::new(("server.tld", SUBMISSION_PORT)).unwrap()
    // Set the name sent during EHLO/HELO, default is `localhost`
    .hello_name("my.hostname.tld")
    // Add credentials for authentication
    .credentials("username", "password")
    // Use TLS with STARTTLS, you can also specify a specific SSL context
    // with `.ssl_context(context)`
    .starttls()
    // Configure accepted authetication mecanisms
    .authentication_mecanisms(vec![Mecanism::CramMd5])
    // Enable connection reuse
    .enable_connection_reuse(true).build();

let result_1 = sender.send(email.clone());
assert!(result_1.is_ok());

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

// Explicitely close the SMTP transaction as we enabled connection reuse
sender.close();

Using the client directly

If you just want to send an email without using Email to provide headers:

use smtp::sender::{Sender, SenderBuilder};
use smtp::email::SimpleSendableEmail;

// Create a minimal email
let email = SimpleSendableEmail::new(
    "test@example.com",
    "test@example.org",
    "Hello world !"
);

let mut sender = SenderBuilder::localhost().unwrap().build();
let result = sender.send(email);
assert!(result.is_ok());

Lower level

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

use smtp::client::Client;
use smtp::SMTP_PORT;
use smtp::client::net::NetworkStream;

let mut email_client: Client<NetworkStream> = Client::new();
let _ = email_client.connect(&("localhost", SMTP_PORT));
let _ = email_client.ehlo("my_hostname");
let _ = email_client.mail("user@example.com", None);
let _ = email_client.rcpt("user@example.org");
let _ = email_client.data();
let _ = email_client.message("Test email");
let _ = email_client.quit();

Dependencies

~9MB
~151K SLoC