Crate maik

Crate maik 

Source
Expand description

maik is a mock SMTP server library. It is designed to help write integration tests for applications that send emails.

§Examples

Basic example with authentication and TLS, using the lettre client.

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

#[test]
fn send_with_auth_and_tls() {
    // set up and start the mock server
    let server = MockServer::builder()
        .add_mailbox("user@domain.tld", "my_password")
        .build();

    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("user@domain.tld"), 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("user@domain.tld".parse().unwrap())
        .to("danger@domain.tld".parse().unwrap())
        .body(String::from("What's up?"))
        .unwrap();
    mailer.send(&message);

    // assert user@domain.tld sent "What's up?" to danger@domain.tld
    let ma = MailAssertion::new()
        .sender_is("user@domain.tld")
        .recipients_are(["danger@domain.tld"])
        .body_is("What's up?");
    assert!(server.assert(ma));
}

Another example using the lettre client, without tls and verification of authentication credentials.

use lettre::{
    message::Mailboxes,
    transport::smtp::{
        authentication::{Credentials, Mechanism},
        client::{Certificate, Tls},
        SmtpTransport,
    },
    Message, Transport,
};
use maik::{MailAssertion, MockServer};
use regex::bytes::Regex;

#[test]
fn no_verify_credentials_no_tls() {
    // set up and start the mock server
    let server = MockServer::builder()
        .no_verify_credentials()
        .build();

    server.start();

    // set up the client using lettre
    let credentials = Credentials::new(
        String::from("no-reply@domain.tld"),
        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("no-reply@domain.tld".parse().unwrap())
        .to("user@domain.tld".parse().unwrap())
        .body(String::from("Here is your verification code: 123456"))
        .unwrap();
    mailer.send(&message);

    // assert user@domain.tld received some mail that ends with "verification code: \d{6}"
    let ma = MailAssertion::new()
        .recipients_are(["user@domain.tld"])
        .body_matches(Regex::new(r"verification code: \d{6}$").unwrap());
    assert!(server.assert(ma));
}

Structs§

MailAssertion
An assertion object for testing the mails sent on a MockServer. See MockServer::assert for calling the MailAssertion.
MockServer
A mock SMTP server.
MockServerBuilder
Builder for configuring and creating MockServer.