From e46b9dcfa1c44a1b77b84e60d7de8d0993a49b67 Mon Sep 17 00:00:00 2001 From: Remy Vuong Date: Tue, 19 May 2020 18:24:08 +0200 Subject: [PATCH 1/2] Added: STARTTLS example --- examples/starttls.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 examples/starttls.rs diff --git a/examples/starttls.rs b/examples/starttls.rs new file mode 100644 index 0000000..7f1588d --- /dev/null +++ b/examples/starttls.rs @@ -0,0 +1,68 @@ +/** + * Here's an example showing how to connect to the IMAP server with STARTTLS. + * The only difference with the `basic.rs` example is when using `imap::connect_starttls()` method + * instead of `imap::connect()` (l. 52) + * + * The following env vars are expected to be set: + * - IMAP_HOST + * - IMAP_USERNAME + * - IMAP_PASSWORD + * - IMAP_PORT (supposed to be 143) + */ + +extern crate imap; +extern crate native_tls; + +use native_tls::TlsConnector; +use std::env; +use std::error::Error; + +fn main() -> Result<(), Box> { + let imap_host = env::var("IMAP_HOST") + .expect("Missing or invalid env var: IMAP_HOST"); + let imap_username = env::var("IMAP_USERNAME") + .expect("Missing or invalid env var: IMAP_USERNAME"); + let imap_password = env::var("IMAP_PASSWORD") + .expect("Missing or invalid env var: IMAP_PASSWORD"); + let imap_port: u16 = env::var("IMAP_PORT") + .expect("Missing or invalid env var: IMAP_PORT") + .to_string() + .parse() + .unwrap(); + + if let Some(_email) = fetch_inbox_top(imap_host, imap_username, imap_password, imap_port)? { + eprintln!("OK :)"); + } + + Ok(()) +} + +fn fetch_inbox_top( + host: String, + username: String, + password: String, + port: u16, +) -> Result, Box> { + let domain: &str = host.as_str(); + + let tls = TlsConnector::builder().build().unwrap(); + + // we pass in the domain twice to check that the server's TLS + // certificate is valid for the domain we're connecting to. + let client = imap::connect_starttls( + (domain, port), + domain, + &tls, + ).unwrap(); + + // the client we have here is unauthenticated. + // to do anything useful with the e-mails, we need to log in + let mut _imap_session = client + .login(username.as_str(), password.as_str()) + .map_err(|e| e.0)?; + + // TODO Here you can process as you want. eg. search/fetch messages according to your needs. + + // This returns `Ok(None)` for the need of the example + Ok(None) +} From 1b3840187826ce7e68b2b1feb1bced8532292cf1 Mon Sep 17 00:00:00 2001 From: Remy Vuong Date: Wed, 20 May 2020 08:34:13 +0200 Subject: [PATCH 2/2] Changed: description update, plus code formatting --- examples/starttls.rs | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/examples/starttls.rs b/examples/starttls.rs index 7f1588d..071021f 100644 --- a/examples/starttls.rs +++ b/examples/starttls.rs @@ -1,7 +1,8 @@ /** * Here's an example showing how to connect to the IMAP server with STARTTLS. * The only difference with the `basic.rs` example is when using `imap::connect_starttls()` method - * instead of `imap::connect()` (l. 52) + * instead of `imap::connect()` (l. 52), and so you can connect on port 143 instead of 993 + * as you have to when using TLS the entire way. * * The following env vars are expected to be set: * - IMAP_HOST @@ -9,7 +10,6 @@ * - IMAP_PASSWORD * - IMAP_PORT (supposed to be 143) */ - extern crate imap; extern crate native_tls; @@ -18,12 +18,11 @@ use std::env; use std::error::Error; fn main() -> Result<(), Box> { - let imap_host = env::var("IMAP_HOST") - .expect("Missing or invalid env var: IMAP_HOST"); - let imap_username = env::var("IMAP_USERNAME") - .expect("Missing or invalid env var: IMAP_USERNAME"); - let imap_password = env::var("IMAP_PASSWORD") - .expect("Missing or invalid env var: IMAP_PASSWORD"); + let imap_host = env::var("IMAP_HOST").expect("Missing or invalid env var: IMAP_HOST"); + let imap_username = + env::var("IMAP_USERNAME").expect("Missing or invalid env var: IMAP_USERNAME"); + let imap_password = + env::var("IMAP_PASSWORD").expect("Missing or invalid env var: IMAP_PASSWORD"); let imap_port: u16 = env::var("IMAP_PORT") .expect("Missing or invalid env var: IMAP_PORT") .to_string() @@ -49,11 +48,7 @@ fn fetch_inbox_top( // we pass in the domain twice to check that the server's TLS // certificate is valid for the domain we're connecting to. - let client = imap::connect_starttls( - (domain, port), - domain, - &tls, - ).unwrap(); + let client = imap::connect_starttls((domain, port), domain, &tls).unwrap(); // the client we have here is unauthenticated. // to do anything useful with the e-mails, we need to log in