#smtp #dkim #email #mime #mail

mail-send

E-mail delivery library with SMTP and DKIM support

5 releases

0.2.3 Sep 21, 2022
0.2.2 Aug 31, 2022
0.2.1 Aug 10, 2022
0.2.0 May 31, 2022
0.1.0 May 30, 2022

#48 in Email

Download history 19/week @ 2022-08-18 26/week @ 2022-08-25 11/week @ 2022-09-01 17/week @ 2022-09-08 28/week @ 2022-09-15 22/week @ 2022-09-22 14/week @ 2022-09-29 19/week @ 2022-10-06 36/week @ 2022-10-13 70/week @ 2022-10-20 414/week @ 2022-10-27 291/week @ 2022-11-03 267/week @ 2022-11-10 176/week @ 2022-11-17 162/week @ 2022-11-24 87/week @ 2022-12-01

761 downloads per month

Apache-2.0 OR MIT

100KB
2K SLoC

mail-send

crates.io build docs.rs crates.io

mail-send is a Rust library to build, sign and send e-mail messages via SMTP. It includes the following features:

  • Generates e-mail messages conforming to the Internet Message Format standard (RFC 5322).
  • Full MIME support (RFC 2045 - 2049) with automatic selection of the most optimal encoding for each message body part.
  • DomainKeys Identified Mail (DKIM) Signatures (RFC 6376).
  • Simple Mail Transfer Protocol (SMTP; RFC 5321) delivery.
  • SMTP Service Extension for Secure SMTP over TLS (RFC 3207).
  • SMTP Service Extension for Authentication (RFC 4954) with automatic mechanism negotiation (from most secure to least secure):
    • CRAM-MD5 (RFC 2195)
    • DIGEST-MD5 (RFC 2831; obsolete but still supported)
    • XOAUTH2 (Google proprietary)
    • LOGIN
    • PLAIN
  • Full async (requires Tokio).

Usage Example

Send a message via an SMTP server that requires authentication:

    // Build a simple multipart message
    let message = MessageBuilder::new()
        .from(("John Doe", "john@example.com"))
        .to(vec![
            ("Jane Doe", "jane@example.com"),
            ("James Smith", "james@test.com"),
        ])
        .subject("Hi!")
        .html_body("<h1>Hello, world!</h1>")
        .text_body("Hello world!");

    // Connect to an SMTP relay server over TLS and
    // authenticate using the provided credentials.
    Transport::new("smtp.gmail.com")
        .credentials("john", "p4ssw0rd")
        .connect_tls()
        .await
        .unwrap()
        .send(message)
        .await
        .unwrap();

Sign a message with DKIM and send it via an SMTP relay server:

    // Build a simple text message with a single attachment
    let message = MessageBuilder::new()
        .from(("John Doe", "john@example.com"))
        .to("jane@example.com")
        .subject("Howdy!")
        .text_body("These pretzels are making me thirsty.")
        .binary_attachment("image/png", "pretzels.png", [1, 2, 3, 4].as_ref());

    // Set up DKIM signer
    let dkim = DKIM::from_pkcs1_pem_file("./cert.pem")
        .unwrap()
        .domain("example.com")
        .selector("2022")
        .headers(["From", "To", "Subject"]) // Headers to sign
        .expiration(60 * 60 * 7); // Number of seconds before this signature expires (optional)

    // Connect to an SMTP relay server over TLS.
    // Signs each message with the configured DKIM signer.
    Transport::new("smtp.example.com")
        .dkim(dkim)
        .connect_tls()
        .await
        .unwrap()
        .send(message)
        .await
        .unwrap();

Send a message via an unsecured SMTP listening on port 2525. Mail-send will automatically upgrade the connection to TLS if the server advertises the STARTTLS extension:

    // Build a simple multipart message
    let message = MessageBuilder::new()
        .from(("John Doe", "john@example.com"))
        .to(vec![
            ("Jane Doe", "jane@example.com"),
            ("James Smith", "james@test.com"),
        ])
        .subject("Hi!")
        .html_body("<h1>Hello, world!</h1>")
        .text_body("Hello world!");

    // Send the message
    Transport::new("unsecured.example.com")
        .port(2525)
        .connect()
        .await
        .unwrap()
        .send(message)
        .await
        .unwrap();

More examples of how to build messages are available in the mail-builder crate. Please note that this library does not support parsing e-mail messages as this functionality is provided separately by the mail-parser crate.

Testing

To run the testsuite:

 $ cargo test --all-features

or, to run the testsuite with MIRI:

 $ cargo +nightly miri test --all-features

License

Licensed under either of

at your option.

Copyright (C) 2020-2022, Stalwart Labs Ltd.

See COPYING for the license.

Dependencies

~9–42MB
~764K SLoC