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
Use rayon's find_any
Amaury Martiny554ad9ad178f
diff --git a/src/lib.rs b/src/lib.rsindex ecea9a29..926d40af 100644--- a/src/lib.rs+++ b/src/lib.rs@@ -36,15 +36,12 @@ pub fn email_exists(from_email: &str, to_email: &str) -> bool {}}- combinations+ match combinations.par_iter() // Parallelize the find_any- .find_any(- |(host, port)| match smtp::email_exists(from_email, to_email, host, *port) {- Ok(val) => {- println!("{}", val);- process::exit(0);- }- _ => false,- },- ).is_some()+ .filter_map(|(host, port)| smtp::email_exists(from_email, to_email, host, *port))+ .find_any(|_| true)+ {+ Some(result) => result,+ None => false,+ }}diff --git a/src/smtp.rs b/src/smtp.rsindex 4183d0b2..031d5ec2 100644--- a/src/smtp.rs+++ b/src/smtp.rs@@ -14,13 +14,13 @@ macro_rules! try_smtp (Err(err) => {debug!("Closing {}:{}, because of error '{}'.", $host, $port, err);$client.close();- return Err(());+ return None;},}}));-pub fn email_exists(from: &str, to: &str, host: &Name, port: u16) -> Result<bool, ()> {+pub fn email_exists(from: &str, to: &str, host: &Name, port: u16) -> Option<bool> {debug!("Connecting to {}:{}...", host, port);let mut smtp_client: InnerClient<NetworkStream> = InnerClient::new();@@ -70,19 +70,19 @@ pub fn email_exists(from: &str, to: &str, host: &Name, port: u16) -> Result<boolSome(message) => {// 250 2.1.0 Sender e-mail address ok.if message.contains("2.1.5") {- Ok(true)+ Some(true)} else {- Err(())+ None}}- _ => Err(()),+ _ => None,},Err(err) => {// 550 5.1.1 Mailbox does not exist.if err.to_string().contains("5.1.1") {- Ok(false)+ Some(false)} else {- Err(())+ None}}};@@ -91,8 +91,8 @@ pub fn email_exists(from: &str, to: &str, host: &Name, port: u16) -> Result<boolsmtp_client.close();match result {- Ok(val) => debug!("Checked email on {}:{}, exists={}.", host, port, val),- Err(_) => debug!("Cannot check email on {}:{}.", host, port),+ Some(val) => debug!("Checked email on {}:{}, exists={}.", host, port, val),+ None => debug!("Cannot check email on {}:{}.", host, port),};result}