Skip to content
Projets

À propos

E-mail delivery library for Rust with DKIM support

Productivité et automatisation

Comment ces chiffres sont mesurés

Progression mesurée des étoiles

in 24h
0
in 7 days
+1
in 15 days
+3
in 1 month
Historique en cours

Variations nettes sur des périodes qui se chevauchent, pas des totaux quotidiens. Les mesures absentes restent vides. Les étoiles indiquent l’intérêt, pas la qualité.

README.md

Original

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.
  • DKIMv1 (DomainKeys Identified Mail) Signatures (RFC 6376) with ED25519-SHA256, RSA-SHA256 and RSA-SHA1 support.
  • DKIMv2 Signatures with per-hop envelope binding.
  • 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:

    use mail_send::{SmtpClientBuilder, mail_builder::MessageBuilder};

    // Build a simple multipart message
    let message = MessageBuilder::new()
        .from(("John Doe", "[email protected]"))
        .to(vec![
            ("Jane Doe", "[email protected]"),
            ("James Smith", "[email protected]"),
        ])
        .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)
        .unwrap()
        .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:

    use mail_send::{
        SmtpClientBuilder,
        mail_auth::{
            crypto::{RsaKey, Sha256},
            dkim::DkimSigner,
        },
        mail_builder::MessageBuilder,
    };
    use rustls_pki_types::{PrivateKeyDer, pem::PemObject};
    use std::time::Duration;

    // Build a simple text message with a single attachment
    let message = MessageBuilder::new()
        .from(("John Doe", "[email protected]"))
        .to("[email protected]")
        .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_key_der(PrivateKeyDer::from_pem_slice(TEST_KEY.as_bytes()).unwrap())
            .unwrap();
    let signer = DkimSigner::from_key(pk_rsa)
        .domain("example.com")
        .selector("default")
        .headers(["From", "To", "Subject"])
        .expiration(Duration::from_secs(60 * 60 * 7)); // Signature lifetime (optional)

    // Connect to an SMTP relay server over TLS.
    // Signs each message with the configured DKIM signer.
    SmtpClientBuilder::new("smtp.gmail.com", 465)
        .unwrap()
        .connect()
        .await
        .unwrap()
        .send_signed(message, &signer)
        .await
        .unwrap();

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

    use mail_send::{
        SmtpClientBuilder,
        mail_auth::{
            crypto::{RsaKey, Sha256},
            dkim2::Dkim2Signer,
        },
        mail_builder::MessageBuilder,
    };
    use rustls_pki_types::{PrivateKeyDer, pem::PemObject};

    // Build a simple text message
    let message = MessageBuilder::new()
        .from(("John Doe", "[email protected]"))
        .to("[email protected]")
        .subject("Howdy!")
        .text_body("These pretzels are making me thirsty.");

    // Sign an e-mail message using RSA-SHA256
    let pk_rsa =
        RsaKey::<Sha256>::from_key_der(PrivateKeyDer::from_pem_slice(TEST_KEY.as_bytes()).unwrap())
            .unwrap();
    let signer = Dkim2Signer::from_key(pk_rsa)
        .domain("example.com")
        .selector("default");

    // Connect to an SMTP relay server over TLS.
    // Signs each message with the configured DKIM2 signer, binding the
    // signature to the SMTP envelope of this hop.
    SmtpClientBuilder::new("smtp.gmail.com", 465)
        .unwrap()
        .connect()
        .await
        .unwrap()
        .send_signed_dkim2(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

Copyright (C) 2020, Stalwart Labs LLC

Explorer des projets similaires

Chargement…