#send-email #smtp #dkim #mime #send-message

mail-send

E-mail delivery library with SMTP and DKIM support

19 releases

0.4.9 Jul 3, 2024
0.4.7 Mar 5, 2024
0.4.6 Dec 30, 2023
0.4.1 Oct 3, 2023
0.2.0 May 31, 2022

#72 in Email

Download history 1078/week @ 2024-07-21 1752/week @ 2024-07-28 1072/week @ 2024-08-04 1321/week @ 2024-08-11 1120/week @ 2024-08-18 1380/week @ 2024-08-25 1441/week @ 2024-09-01 1196/week @ 2024-09-08 1439/week @ 2024-09-15 1490/week @ 2024-09-22 1288/week @ 2024-09-29 1520/week @ 2024-10-06 1602/week @ 2024-10-13 2068/week @ 2024-10-20 1488/week @ 2024-10-27 1176/week @ 2024-11-03

6,486 downloads per month
Used in 11 crates (6 directly)

Apache-2.0 OR MIT

74KB
1.5K 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) with ED25519-SHA256, RSA-SHA256 and RSA-SHA1 support.
  • 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 the SMTP submissions port, upgrade to TLS and
    // authenticate using the provided credentials.
    SmtpClientBuilder::new("smtp.gmail.com", 587)
        .implicit_tls(false)
        .credentials(("john", "p4ssw0rd"))
        .connect()
        .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.")
        .attachment("image/png", "pretzels.png", [1, 2, 3, 4].as_ref());

    // Sign an e-mail message using RSA-SHA256
    let pk_rsa = RsaKey::<Sha256>::from_rsa_pem(TEST_KEY).unwrap();
    let signer = DkimSigner::from_key(pk_rsa)
        .domain("example.com")
        .selector("default")
        .headers(["From", "To", "Subject"])
        .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.
    SmtpClientBuilder::new("smtp.gmail.com", 465)
        .connect()
        .await
        .unwrap()
        .send_signed(message, &signer)
        .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.

Dependencies

~11–39MB
~787K SLoC