Skip to content

Commit beb41d2

Browse files
committed
(feat) Provide example integration with Rustls crate
By checking this code in as an example, we can prevent bitrot as we continuously test this in CI. It also demonstrates how to setup TLS without relying on platform specific dependencies via native_tls crate.
1 parent 281d2eb commit beb41d2

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,7 @@ chrono = "0.4"
3232
lazy_static = "1.4"
3333

3434
[dev-dependencies]
35+
dotenv = "0.14.1"
3536
lettre = "0.9"
3637
lettre_email = "0.9"
38+
rustls-connector = "0.8.0"

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ This directory contains examples of working with the IMAP client.
66
Examples:
77
* basic - This is a very basic example of using the client.
88
* gmail_oauth2 - This is an example using oauth2 for logging into gmail as a secure appplication.
9+
* rustls - This demonstrates how to use Rustls instead of Openssl for secure connections (helpful for cross compilation).

examples/rustls.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
extern crate imap;
2+
extern crate rustls_connector;
3+
4+
use std::{
5+
env,
6+
error::Error,
7+
net::TcpStream,
8+
};
9+
10+
use dotenv::dotenv;
11+
use rustls_connector::RustlsConnector;
12+
13+
14+
fn main() -> Result<(), Box<dyn Error>> {
15+
// Read config from environment or .env file
16+
dotenv().ok();
17+
let host = env::var("HOST").expect("missing envvar host");
18+
let user = env::var("MAILUSER").expect("missing envvar USER");
19+
let password = env::var("PASSWORD").expect("missing envvar password");
20+
let port = 993;
21+
22+
if let Some(email) = fetch_inbox_top(host, user, password, port)? {
23+
println!("{}", &email);
24+
}
25+
26+
Ok(())
27+
}
28+
29+
fn fetch_inbox_top(host: String, user: String, password: String, port: u16) -> Result<Option<String>, Box<dyn Error>> {
30+
// Setup Rustls TcpStream
31+
let stream = TcpStream::connect((host.as_ref(), port))?;
32+
let tls = RustlsConnector::default();
33+
let tlsstream = tls.connect(&host, stream)?;
34+
35+
// we pass in the domain twice to check that the server's TLS
36+
// certificate is valid for the domain we're connecting to.
37+
let client = imap::Client::new(tlsstream);
38+
39+
// the client we have here is unauthenticated.
40+
// to do anything useful with the e-mails, we need to log in
41+
let mut imap_session = client
42+
.login(&user, &password)
43+
.map_err(|e| e.0)?;
44+
45+
// we want to fetch the first email in the INBOX mailbox
46+
imap_session.select("INBOX")?;
47+
48+
// fetch message number 1 in this mailbox, along with its RFC822 field.
49+
// RFC 822 dictates the format of the body of e-mails
50+
let messages = imap_session.fetch("1", "RFC822")?;
51+
let message = if let Some(m) = messages.iter().next() {
52+
m
53+
} else {
54+
return Ok(None);
55+
};
56+
57+
// extract the message's body
58+
let body = message.body().expect("message did not have a body!");
59+
let body = std::str::from_utf8(body)
60+
.expect("message was not valid utf-8")
61+
.to_string();
62+
63+
// be nice to the server and log out
64+
imap_session.logout()?;
65+
66+
Ok(Some(body))
67+
}

src/client.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ macro_rules! ok_or_unauth_client_err {
222222
impl<T: Read + Write> Client<T> {
223223
/// Creates a new client over the given stream.
224224
///
225+
/// For an example of how to use this method to provide a pure-Rust TLS integration, see the
226+
/// rustls.rs in the examples/ directory.
227+
///
225228
/// This method primarily exists for writing tests that mock the underlying transport, but can
226229
/// also be used to support IMAP over custom tunnels.
227230
pub fn new(stream: T) -> Client<T> {

0 commit comments

Comments
 (0)