1 个不稳定版本

0.1.0 2024 年 7 月 16 日

#110电子邮件

每月 40 次下载

LGPL-3.0-only

31KB
577

maik Rust 文档

适用于 Rust 的模拟 SMTP 服务器库。

支持的标准


lib.rs:

maik 是一个模拟 SMTP 服务器库。它旨在帮助编写发送电子邮件的应用程序的集成测试。

示例

使用带身份验证和 TLS 的基本示例,使用 lettre 客户端。

use lettre::{
    message::Mailboxes,
    transport::smtp::{
        authentication::{Credentials, Mechanism},
        client::{Certificate, Tls, TlsParameters, TlsParametersBuilder},
        SmtpTransport,
    },
    Message, Transport,
};
use maik::{MailAssertion, MockServer};
use std::collections::HashSet;

#[test]
fn send_with_auth_and_tls() {
    // set up and start the mock server
    let mut server = MockServer::new("smtp.domain.tld");
    server.add_mailbox("[email protected]", "my_password");
    server.start();

    // set up the client using lettre
    let certificate = Certificate::from_pem(server.cert_pem()).unwrap();
    let tls_params = TlsParametersBuilder::new(server.host().to_string())
        .add_root_certificate(certificate)
        .build()
        .unwrap();
    let credentials =
        Credentials::new(String::from("[email protected]"), String::from("my_password"));
    let mailer = SmtpTransport::relay(&server.host().to_string())
        .unwrap()
        .port(server.port())
        .tls(Tls::Opportunistic(tls_params))
        .credentials(credentials)
        .authentication(vec![Mechanism::Plain])
        .build();

    // send a mail message
    let message = Message::builder()
        .from("[email protected]".parse().unwrap())
        .to("[email protected]".parse().unwrap())
        .body(String::from("What's up?"))
        .unwrap();
    mailer.send(&message);

    // assert [email protected] sent "What's up?" to [email protected]
    let mut recipients = HashSet::new();
    recipients.insert("[email protected]");

    let ma = MailAssertion::new()
        .sender_is("[email protected]")
        .recipients_are(recipients)
        .body_is("What's up?");
    assert!(server.assert(ma));
}

另一个使用 lettre 客户端的示例,不带 TLS 和验证身份验证凭据。

use lettre::{
    message::Mailboxes,
    transport::smtp::{
        authentication::{Credentials, Mechanism},
        client::{Certificate, Tls},
        SmtpTransport,
    },
    Message, Transport,
};
use maik::{MailAssertion, MockServer};
use std::collections::HashSet;

#[test]
fn no_verify_credentials_no_tls() {
    // set up and start the mock server
    let mut server = MockServer::new("smtp.domain.tld");
    server.set_no_verify_credentials();
    server.start();

    // set up the client using lettre
    let credentials = Credentials::new(
        String::from("[email protected]"),
        String::from("any_password"),
    );
    let mailer = SmtpTransport::relay(&server.host().to_string())
        .unwrap()
        .port(server.port())
        .tls(Tls::None)
        .credentials(credentials)
        .authentication(vec![Mechanism::Plain])
        .build();

    // send a mail message
    let message = Message::builder()
        .from("[email protected]".parse().unwrap())
        .to("[email protected]".parse().unwrap())
        .body(String::from("Here is your verification code: 123456"))
        .unwrap();
    mailer.send(&message);

    // assert [email protected] received some mail that contains "verification code: "
    let mut recipients = HashSet::new();
    recipients.insert("[email protected]");

    let ma = MailAssertion::new()
        .recipients_are(recipients)
        .body_contains("verification code: ");
    assert!(server.assert(ma));
}

依赖项

~9–20MB
~378K SLoC