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
Add ensure_positive
Amaury Martiny7cc208a6e01c
diff --git a/src/main.rs b/src/main.rsindex 42ac6e9b..bf5fddfc 100644--- a/src/main.rs+++ b/src/main.rs@@ -55,7 +55,7 @@ fn main() {|(host, port)| match smtp::email_exists(from_email, to_email, host, *port) {Ok(val) => {println!("{}", val);- process::exit(0x0100);+ process::exit(0);}_ => false,},diff --git a/src/smtp.rs b/src/smtp.rsindex f8660210..60f57d35 100644--- a/src/smtp.rs+++ b/src/smtp.rs@@ -6,18 +6,12 @@ use lettre::EmailAddress;use std::time::Duration;use trust_dns_resolver::Name;+// Try to send an smtp command, close if fails.macro_rules! try_smtp (($err: expr, $client: ident) => ({match $err {- Ok(res) => {- if !res.is_positive() {- $client.close();- return Err(());- }- res- },- Err(err) => {- debug!("Error: {}", err);+ Ok(res) => res,+ _ => {$client.close();return Err(());},@@ -25,40 +19,60 @@ macro_rules! try_smtp (}));+// Ensure that the error code we get is okay.+macro_rules! ensure_positive (+ ($res: expr, $client: ident) => ({+ if !$res.is_positive() {+ $client.close();+ return Err(());+ }+ $res+ })+);+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 Err(());- }- // Connect to the host. Start with insecure connection and use `STARTTLS`- // when available+ // Set timeout.try_smtp!(- email_client.connect(&(host.to_utf8().as_str(), port), None),+ email_client.set_timeout(Some(Duration::new(3, 0))),+ email_client+ );++ // Connect to the host.+ ensure_positive!(+ try_smtp!(+ email_client.connect(&(host.to_utf8().as_str(), port), None),+ email_client+ ),email_client);- // Send ehlo and get server info- let ehlo_response = try_smtp!(- email_client.command(EhloCommand::new(ClientId::new("localhost".to_string()))),+ // Send ehlo and get server info.+ let ehlo_response = ensure_positive!(+ try_smtp!(+ email_client.command(EhloCommand::new(ClientId::new("localhost".to_string()))),+ email_client+ ),email_client);let server_info = ServerInfo::from_response(&ehlo_response);debug!("Server info: {}", server_info.as_ref().unwrap());- // Send from- try_smtp!(- email_client.command(MailCommand::new(- Some(EmailAddress::new(from.to_string()).unwrap()),- vec![],- )),+ // Send from.+ ensure_positive!(+ try_smtp!(+ email_client.command(MailCommand::new(+ Some(EmailAddress::new(from.to_string()).unwrap()),+ vec![],+ )),+ email_client+ ),email_client);- // Send to+ // Send to.let rctp_response = try_smtp!(email_client.command(RcptCommand::new(EmailAddress::new(to.to_string()).unwrap(),@@ -67,7 +81,7 @@ pub fn email_exists(from: &str, to: &str, host: &Name, port: u16) -> Result<boolemail_client);- // Quit+ // Quit.try_smtp!(email_client.command(QuitCommand), email_client);if let Some(message) = rctp_response.first_line() {@@ -77,7 +91,7 @@ pub fn email_exists(from: &str, to: &str, host: &Name, port: u16) -> Result<bool}// 550 5.1.1 Mailbox does not exist.if message.contains("5.1.1") {- return Ok(true);+ return Ok(false);}}