Skip to content

Commit a890f98

Browse files
test: switch to async-smtp
1 parent 6797ca0 commit a890f98

File tree

2 files changed

+32
-50
lines changed

2 files changed

+32
-50
lines changed

Cargo.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,9 @@ lazy_static = "1.4.0"
3838
log = "0.4.8"
3939

4040
[dev-dependencies]
41-
lettre = "0.9"
4241
lettre_email = "0.9"
43-
rustls-connector = "0.8.0"
44-
rustls = { version = "0.16.0", features = ["dangerous_configuration"] }
45-
webpki = "0.21.0"
4642
pretty_assertions = "0.6.1"
47-
native-tls = "0.2.3"
43+
async-smtp = "0.2.0"
4844

4945
[[example]]
5046
name = "basic"

tests/imap_integration.rs

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,11 @@ use async_native_tls::TlsConnector;
55
use async_std::net::TcpStream;
66
use async_std::prelude::*;
77
use async_std::task;
8-
use lettre::Transport;
98

10-
fn native_tls() -> native_tls::TlsConnector {
11-
native_tls::TlsConnector::builder()
9+
fn native_tls() -> async_native_tls::TlsConnector {
10+
async_native_tls::TlsConnector::new()
1211
.danger_accept_invalid_certs(true)
1312
.danger_accept_invalid_hostnames(true)
14-
.build()
15-
.unwrap()
16-
}
17-
18-
pub struct NoCertificateVerification {}
19-
20-
impl rustls::ServerCertVerifier for NoCertificateVerification {
21-
fn verify_server_cert(
22-
&self,
23-
_roots: &rustls::RootCertStore,
24-
_presented_certs: &[rustls::Certificate],
25-
_dns_name: webpki::DNSNameRef<'_>,
26-
_ocsp: &[u8],
27-
) -> Result<rustls::ServerCertVerified, rustls::TLSError> {
28-
Ok(rustls::ServerCertVerified::assertion())
29-
}
3013
}
3114

3215
fn tls() -> TlsConnector {
@@ -54,21 +37,23 @@ async fn session(user: &str) -> Session<async_native_tls::TlsStream<TcpStream>>
5437
s
5538
}
5639

57-
fn smtp(user: &str) -> lettre::SmtpTransport {
58-
let creds = lettre::smtp::authentication::Credentials::new(user.to_string(), user.to_string());
59-
lettre::SmtpClient::new(
40+
async fn smtp(user: &str) -> async_smtp::SmtpTransport {
41+
let creds =
42+
async_smtp::smtp::authentication::Credentials::new(user.to_string(), user.to_string());
43+
async_smtp::SmtpClient::with_security(
6044
&format!(
6145
"{}:3465",
6246
std::env::var("TEST_HOST").unwrap_or("127.0.0.1".to_string())
6347
),
64-
lettre::ClientSecurity::Wrapper(lettre::ClientTlsParameters {
48+
async_smtp::ClientSecurity::Wrapper(async_smtp::ClientTlsParameters {
6549
connector: native_tls(),
66-
domain: "smpt.example.com".to_string(),
50+
domain: "localhost".to_string(),
6751
}),
6852
)
69-
.unwrap()
53+
.await
54+
.expect("Failed to connect to smtp server")
7055
.credentials(creds)
71-
.transport()
56+
.into_transport()
7257
}
7358

7459
// #[test]
@@ -130,6 +115,18 @@ fn inbox_zero() {
130115
assert_eq!(inbox.len(), 0);
131116
});
132117
}
118+
fn make_email(to: &str) -> async_smtp::SendableEmail {
119+
let message_id = "abc";
120+
async_smtp::SendableEmail::new(
121+
async_smtp::Envelope::new(
122+
Some("sender@localhost".parse().unwrap()),
123+
vec![to.parse().unwrap()],
124+
)
125+
.unwrap(),
126+
message_id.to_string(),
127+
format!("To: <{}>\r\nFrom: <sender@localhost>\r\nMessage-ID: <{}.msg@localhost>\r\nSubject: My first e-mail\r\n\r\nHello world from SMTP", to, message_id),
128+
)
129+
}
133130

134131
#[test]
135132
#[ignore]
@@ -142,16 +139,11 @@ fn inbox() {
142139
c.select("INBOX").await.unwrap();
143140

144141
println!("sending");
142+
let mut s = smtp(to).await;
143+
145144
// then send the e-mail
146-
let mut s = smtp(to);
147-
let e = lettre_email::Email::builder()
148-
.from("sender@localhost")
149-
.to(to)
150-
.subject("My first e-mail")
151-
.text("Hello world from SMTP")
152-
.build()
153-
.unwrap();
154-
s.send(e.into()).unwrap();
145+
let mail = make_email(to);
146+
s.connect_and_send(mail).await.unwrap();
155147

156148
println!("searching");
157149

@@ -191,7 +183,7 @@ fn inbox() {
191183
let fetch = &fetch[0];
192184
assert_eq!(fetch.message, 1);
193185
assert_ne!(fetch.uid, None);
194-
assert_eq!(fetch.size, Some(138));
186+
assert_eq!(fetch.size, Some(21));
195187
let e = fetch.envelope().unwrap();
196188
assert_eq!(e.subject, Some(&b"My first e-mail"[..]));
197189
assert_ne!(e.from, None);
@@ -232,15 +224,9 @@ fn inbox_uid() {
232224
c.select("INBOX").await.unwrap();
233225

234226
// then send the e-mail
235-
let mut s = smtp(to);
236-
let e = lettre_email::Email::builder()
237-
.from("sender@localhost")
238-
.to(to)
239-
.subject("My first e-mail")
240-
.text("Hello world from SMTP")
241-
.build()
242-
.unwrap();
243-
s.send(e.into()).unwrap();
227+
let mut s = smtp(to).await;
228+
let e = make_email(to);
229+
s.connect_and_send(e).await.unwrap();
244230

245231
// now we should see the e-mail!
246232
let inbox = c.uid_search("ALL").await.unwrap();

0 commit comments

Comments
 (0)