Check if an email address exists without sending any email, written in Rust. Comes with a ⚙️ HTTP backend.
Rust emailemail-validationemail-validation-apiemail-verificationemail-verification-apiemail-verifierhacktoberfest
Return result instead of bool
Amaury Martiny12754fe361f0
diff --git a/src/main.rs b/src/main.rsindex 1e4e5817..51fcfae8 100644--- a/src/main.rs+++ b/src/main.rs@@ -51,8 +51,12 @@ fn main() {let found = combinations.par_iter() // Parallelize the find_any- .find_any(|(host, port)| smtp::email_exists(from_email, to_email, host, *port))- .is_some();+ .find_any(+ |(host, port)| match smtp::email_exists(from_email, to_email, host, *port) {+ Ok(val) => val,+ _ => false,+ },+ ).is_some();println!("{}", found);}diff --git a/src/smtp.rs b/src/smtp.rsindex 808aa071..f8660210 100644--- a/src/smtp.rs+++ b/src/smtp.rs@@ -11,30 +11,27 @@ macro_rules! try_smtp (match $err {Ok(res) => {if !res.is_positive() {- if let Some(message) = res.first_line() {- debug!("Error: {}", message);- }$client.close();- return false;+ return Err(());}res},Err(err) => {debug!("Error: {}", err);$client.close();- return false;+ return Err(());},}}));-pub fn email_exists(from: &str, to: &str, host: &Name, port: u16) -> bool {+pub fn email_exists(from: &str, to: &str, host: &Name, port: u16) -> Result<bool, ()> {debug!("Connecting to {}:{}...", host, port);let mut email_client: InnerClient<NetworkStream> = InnerClient::new();if let Err(err) = email_client.set_timeout(Some(Duration::new(1, 0))) {debug!("{}", err);email_client.close();- return false;+ return Err(());}// Connect to the host. Start with insecure connection and use `STARTTLS`@@ -74,10 +71,15 @@ pub fn email_exists(from: &str, to: &str, host: &Name, port: u16) -> bool {try_smtp!(email_client.command(QuitCommand), email_client);if let Some(message) = rctp_response.first_line() {+ // 250 2.1.5 Recipient e-mail address ok.if message.contains("2.1.5") {- return true;+ return Ok(true);+ }+ // 550 5.1.1 Mailbox does not exist.+ if message.contains("5.1.1") {+ return Ok(true);}}- false+ Err(())}