1 unstable release
0.1.0 | Jul 16, 2024 |
---|
#130 in Email
42 downloads per month
31KB
577 lines
maik
A mock SMTP server library for Rust.
Supported standards
lib.rs
:
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};
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("user@domain.tld", "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("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 mut recipients = HashSet::new();
recipients.insert("danger@domain.tld");
let ma = MailAssertion::new()
.sender_is("user@domain.tld")
.recipients_are(recipients)
.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 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("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 contains "verification code: "
let mut recipients = HashSet::new();
recipients.insert("user@domain.tld");
let ma = MailAssertion::new()
.recipients_are(recipients)
.body_contains("verification code: ");
assert!(server.assert(ma));
}
Dependencies
~9–20MB
~380K SLoC