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
fix: Put Smtp debug details in Debug struct
Amaury5b71ca59b6fa
diff --git a/backend/openapi.json b/backend/openapi.jsonindex 09fae414..65caef34 100644--- a/backend/openapi.json+++ b/backend/openapi.json@@ -277,9 +277,6 @@"is_disabled": {"type": "boolean","description": "Has this email address been disabled by the email provider?"- },- "verif_method": {- "$ref": "#/components/schemas/VerifMethod"}},"required": [@@ -287,8 +284,7 @@"has_full_inbox","is_catch_all","is_deliverable",- "is_disabled",- "verif_method"+ "is_disabled"]},"SyntaxDetails": {@@ -326,7 +322,7 @@"HotmailVerifMethod": {"type": "string","x-stoplight": {- "id": "tiy37w0jp5x13"+ "id": "ntdugsleyotut"},"title": "HotmailVerifMethod","enum": ["Api", "Headless", "Smtp"],@@ -335,7 +331,7 @@"GmailVerifMethod": {"type": "string","x-stoplight": {- "id": "2p7yqzxi6n3bj"+ "id": "xo5r48yhtxiwr"},"title": "GmailVerifMethod","enum": ["Api", "Smtp"],@@ -430,12 +426,56 @@"description": "Password to pass to proxy authentication."}},- "required": ["host", "port"]+ "required": ["host", "port"],+ "description": "Proxy information for email verification."+ },+ "DebugDetails": {+ "title": "DebugDetails",+ "x-stoplight": {+ "id": "4wxlk39h8v9kz"+ },+ "type": "object",+ "properties": {+ "start_time": {+ "type": "string",+ "x-stoplight": {+ "id": "60i65k60m8e8d"+ },+ "description": "The time when the email verification started."+ },+ "end_time": {+ "type": "string",+ "x-stoplight": {+ "id": "zw4ccbvwsoh6q"+ },+ "description": "The time when the email verification ended."+ },+ "duration": {+ "$ref": "#/components/schemas/Duration"+ },+ "server_name": {+ "type": "string",+ "x-stoplight": {+ "id": "2jrbdecvqh4t5"+ },+ "description": "The name of the server that performed the email verification."+ },+ "smtp": {+ "$ref": "#/components/schemas/DebugDetailsSmtp"+ }+ },+ "required": [+ "start_time",+ "end_time",+ "duration",+ "server_name",+ "smtp"+ ]},"Duration": {"title": "Duration","x-stoplight": {- "id": "kwbhwi9fkpg6q"+ "id": "bg9y0iez4zati"},"type": "object","description": "An object representing a duration (seconds + nanoseconds).",@@ -451,46 +491,29 @@},"required": ["secs", "nanos"]},- "DebugDetails": {- "title": "DebugDetails",+ "DebugDetailsSmtp": {+ "title": "DebugDetailsSmtp","x-stoplight": {- "id": "jhror15zeepgx"+ "id": "2a90bzapppo0j"},"type": "object","properties": {- "start_time": {- "type": "string",- "x-stoplight": {- "id": "ms8ftx9vwth1a"- }- },- "end_time": {- "type": "string",- "x-stoplight": {- "id": "g2kydtwtmr7po"- }- },- "duration": {- "$ref": "#/components/schemas/Duration"- },- "server_ip": {- "type": "string",- "x-stoplight": {- "id": "ktn104e8oyu6d"- }+ "verif_method": {+ "$ref": "#/components/schemas/VerifMethod"}- }+ },+ "description": "Smtp details used for debugging, such as which method is used."},"VerifMethod": {"title": "VerifMethod","x-stoplight": {- "id": "yfa2hjmyy321x"+ "id": "9xw9e1jwti230"},"type": "object","properties": {"type": {"x-stoplight": {- "id": "dzmw4acub9xp6"+ "id": "4ogsz639tcdb6"},"enum": ["Smtp", "Headless", "Api", "Skipped"],"description": "The method used to perform the email verification"@@ -498,19 +521,20 @@"host": {"type": "string","x-stoplight": {- "id": "nvnjrm1137x96"+ "id": "lxbxckircn0sk"},"description": "If `type` is `SmtpConnection`, the hostname that Reacher connected to."},"port": {"type": "number","x-stoplight": {- "id": "xpxzvllzxxyyn"+ "id": "xqfdjyoiodq23"},"description": "If `type` is `SmtpConnection`, the port that Reacher connected to."}},- "required": ["type"]+ "required": ["type"],+ "description": "The verification method used for the email."}},"securitySchemes": {diff --git a/backend/tests/check_email.rs b/backend/tests/check_email.rsindex e2c46c79..add48e09 100644--- a/backend/tests/check_email.rs+++ b/backend/tests/check_email.rs@@ -23,8 +23,8 @@ use reacher_backend::routes::create_routes;use warp::http::StatusCode;use warp::test::request;-const FOO_BAR_RESPONSE: &str = r#"{"input":"foo@bar","is_reachable":"invalid","misc":{"is_disposable":false,"is_role_account":false,"gravatar_url":null,"haveibeenpwned":null},"mx":{"accepts_mail":false,"records":[]},"smtp":{"can_connect_smtp":false,"has_full_inbox":false,"is_catch_all":false,"is_deliverable":false,"is_disabled":false,"verif_method":{"type":"Skipped"}},"syntax":{"address":null,"domain":"","is_valid_syntax":false,"username":"","normalized_email":null,"suggestion":null}"#;-const FOO_BAR_BAZ_RESPONSE: &str = r#"{"input":"[email protected]","is_reachable":"invalid","misc":{"is_disposable":false,"is_role_account":false,"gravatar_url":null,"haveibeenpwned":null},"mx":{"accepts_mail":false,"records":[]},"smtp":{"can_connect_smtp":false,"has_full_inbox":false,"is_catch_all":false,"is_deliverable":false,"is_disabled":false,"verif_method":{"type":"Skipped"}},"syntax":{"address":"[email protected]","domain":"bar.baz","is_valid_syntax":true,"username":"foo","normalized_email":"[email protected]","suggestion":null}"#;+const FOO_BAR_RESPONSE: &str = r#"{"input":"foo@bar","is_reachable":"invalid","misc":{"is_disposable":false,"is_role_account":false,"gravatar_url":null,"haveibeenpwned":null},"mx":{"accepts_mail":false,"records":[]},"smtp":{"can_connect_smtp":false,"has_full_inbox":false,"is_catch_all":false,"is_deliverable":false,"is_disabled":false},"syntax":{"address":null,"domain":"","is_valid_syntax":false,"username":"","normalized_email":null,"suggestion":null}"#;+const FOO_BAR_BAZ_RESPONSE: &str = r#"{"input":"[email protected]","is_reachable":"invalid","misc":{"is_disposable":false,"is_role_account":false,"gravatar_url":null,"haveibeenpwned":null},"mx":{"accepts_mail":false,"records":[]},"smtp":{"can_connect_smtp":false,"has_full_inbox":false,"is_catch_all":false,"is_deliverable":false,"is_disabled":false},"syntax":{"address":"[email protected]","domain":"bar.baz","is_valid_syntax":true,"username":"foo","normalized_email":"[email protected]","suggestion":null}"#;#[tokio::test]async fn test_input_foo_bar() {diff --git a/core/src/lib.rs b/core/src/lib.rsindex 157cbed6..b920f5b5 100644--- a/core/src/lib.rs+++ b/core/src/lib.rs@@ -219,7 +219,7 @@ pub async fn check_email(input: &CheckEmailInput) -> CheckEmailOutput {mx_records[mx_records.len() - 1]};- let my_smtp = check_smtp(+ let (my_smtp, smtp_debug) = check_smtp(my_syntax.address.as_ref()@@ -249,6 +249,7 @@ pub async fn check_email(input: &CheckEmailInput) -> CheckEmailOutput {duration: end_time.duration_since(start_time).unwrap_or(Duration::from_secs(0)),+ smtp: smtp_debug,..Default::default()},}diff --git a/core/src/smtp/connect.rs b/core/src/smtp/connect.rsindex 0bae0385..ea35a902 100644--- a/core/src/smtp/connect.rs+++ b/core/src/smtp/connect.rs@@ -26,7 +26,7 @@ use std::iter;use std::str::FromStr;use std::time::Duration;-use super::{parser, SmtpConnection, VerifMethod};+use super::parser;use super::{SmtpDetails, SmtpError};use crate::{rules::{has_rule, Rule},@@ -334,10 +334,6 @@ async fn check_smtp_without_retry(is_catch_all,is_deliverable: deliverability.is_deliverable,is_disabled: deliverability.is_disabled,- verif_method: VerifMethod::Smtp(SmtpConnection {- host: host.to_string(),- port,- }),})}diff --git a/core/src/smtp/mod.rs b/core/src/smtp/mod.rsindex fede1e89..b0f76220 100644--- a/core/src/smtp/mod.rs+++ b/core/src/smtp/mod.rs@@ -43,7 +43,7 @@ use self::{yahoo::is_yahoo,};-#[derive(Debug, Default, Deserialize, Serialize)]+#[derive(Debug, Default, Deserialize, PartialEq, Serialize)]pub struct SmtpConnection {/// The host we connected to via SMTP.pub host: String,@@ -51,7 +51,7 @@ pub struct SmtpConnection {pub port: u16,}-#[derive(Debug, Default, Deserialize, Serialize)]+#[derive(Debug, Default, Deserialize, PartialEq, Serialize)]#[serde(tag = "type")]pub enum VerifMethod {/// Email verification was done via SMTP.@@ -78,6 +78,11 @@ pub struct SmtpDetails {pub is_deliverable: bool,/// Is the email blocked or disabled by the provider?pub is_disabled: bool,+}++#[derive(Debug, Default, Deserialize, Serialize)]+pub struct SmtpDebug {+ /// The verification method used for the email.pub verif_method: VerifMethod,}@@ -89,14 +94,19 @@ pub async fn check_smtp(port: u16,domain: &str,input: &CheckEmailInput,-) -> Result<SmtpDetails, SmtpError> {+) -> (Result<SmtpDetails, SmtpError>, SmtpDebug) {let host = host.to_string();let to_email_str = to_email.to_string();if input.skipped_domains.iter().any(|d| host.contains(d)) {- return Err(SmtpError::SkippedDomain(format!(- "Reacher currently cannot verify emails from @{domain}"- )));+ return (+ Err(SmtpError::SkippedDomain(format!(+ "Reacher currently cannot verify emails from @{domain}"+ ))),+ SmtpDebug {+ verif_method: VerifMethod::Skipped,+ },+ );}let webdriver_addr = env::var("RCH_WEBDRIVER_ADDR");@@ -106,10 +116,24 @@ pub async fn check_smtp((HotmailVerifMethod::OneDriveApi, _) => {if is_microsoft365(&host) {match outlook::microsoft365::check_microsoft365_api(to_email, input).await {- Ok(Some(smtp_details)) => return Ok(smtp_details),+ Ok(Some(smtp_details)) => {+ return {+ (+ Ok(smtp_details),+ SmtpDebug {+ verif_method: VerifMethod::Api,+ },+ )+ }+ }// Continue in the event of an error/ambiguous result.Err(err) => {- return Err(err.into());+ return (+ Err(err.into()),+ SmtpDebug {+ verif_method: VerifMethod::Api,+ },+ );}_ => {}}@@ -117,44 +141,66 @@ pub async fn check_smtp(}#[cfg(feature = "headless")](HotmailVerifMethod::Headless, Ok(a)) => {- return outlook::headless::check_password_recovery(- to_email.to_string().as_str(),- &a,- )- .await- .map_err(|err| err.into());+ return (+ outlook::headless::check_password_recovery(to_email.to_string().as_str(), &a)+ .await+ .map_err(|err| err.into()),+ SmtpDebug {+ verif_method: VerifMethod::Headless,+ },+ );}_ => {}};} else if is_gmail(&host) {if let GmailVerifMethod::Api = &input.gmail_verif_method {- return gmail::check_gmail(to_email, input)- .await- .map_err(|err| err.into());+ return (+ gmail::check_gmail(to_email, input)+ .await+ .map_err(|err| err.into()),+ SmtpDebug {+ verif_method: VerifMethod::Api,+ },+ );};} else if is_yahoo(&host) {match (&input.yahoo_verif_method, webdriver_addr) {(YahooVerifMethod::Api, _) => {- return yahoo::check_api(&to_email_str, input)- .await- .map_err(|e| e.into())+ return (+ yahoo::check_api(&to_email_str, input)+ .await+ .map_err(|e| e.into()),+ SmtpDebug {+ verif_method: VerifMethod::Api,+ },+ )}#[cfg(feature = "headless")](YahooVerifMethod::Headless, Ok(a)) => {- return yahoo::check_headless(&to_email_str, &a)- .await- .map_err(|e| e.into())+ return (+ yahoo::check_headless(&to_email_str, &a)+ .await+ .map_err(|e| e.into()),+ SmtpDebug {+ verif_method: VerifMethod::Headless,+ },+ )}_ => {}};}- check_smtp_with_retry(to_email, &host, port, domain, input, input.retries).await+ (+ check_smtp_with_retry(to_email, &host, port, domain, input, input.retries).await,+ SmtpDebug {+ verif_method: VerifMethod::Smtp(SmtpConnection { host, port }),+ },+ )}#[cfg(test)]mod tests {- use super::{check_smtp, CheckEmailInput, SmtpError};+ use super::{check_smtp, CheckEmailInput, SmtpConnection, SmtpError};use async_smtp::{smtp::error::Error, EmailAddress};use std::{str::FromStr, time::Duration};use tokio::runtime::Runtime;@@ -169,7 +215,15 @@ mod tests {let mut input = CheckEmailInput::default();input.set_smtp_timeout(Some(Duration::from_millis(1)));- let res = runtime.block_on(check_smtp(&to_email, &host, 25, "gmail.com", &input));+ let (res, smtp_debug) =+ runtime.block_on(check_smtp(&to_email, &host, 25, "gmail.com", &input));+ assert_eq!(+ smtp_debug.verif_method,+ super::VerifMethod::Smtp(SmtpConnection {+ host: host.to_string(),+ port: 25+ })+ );match res {Err(SmtpError::SmtpError(Error::Io(_))) => (), // ErrorKind == Timeout_ => panic!("check_smtp did not time out"),@@ -185,7 +239,9 @@ mod tests {let mut input = CheckEmailInput::default();input.set_skipped_domains(vec![".mail.icloud.com.".into()]);- let res = runtime.block_on(check_smtp(&to_email, &host, 25, "icloud.com", &input));+ let (res, smtp_debug) =+ runtime.block_on(check_smtp(&to_email, &host, 25, "icloud.com", &input));+ assert_eq!(smtp_debug.verif_method, super::VerifMethod::Skipped);match res {Err(SmtpError::SkippedDomain(_)) => (),r => panic!("{:?}", r),diff --git a/core/src/smtp/outlook/headless.rs b/core/src/smtp/outlook/headless.rsindex 1011ee41..726fad35 100644--- a/core/src/smtp/outlook/headless.rs+++ b/core/src/smtp/outlook/headless.rs@@ -23,7 +23,7 @@ use futures::TryFutureExt;use crate::{smtp::{headless::{create_headless_client, HeadlessError},- SmtpDetails, VerifMethod,+ SmtpDetails,},LOG_TARGET,};@@ -107,7 +107,6 @@ pub async fn check_password_recovery(is_catch_all: false,is_deliverable,is_disabled: false,- verif_method: VerifMethod::Headless,})}diff --git a/core/src/smtp/outlook/microsoft365.rs b/core/src/smtp/outlook/microsoft365.rsindex 4117dd2a..2948fee4 100644--- a/core/src/smtp/outlook/microsoft365.rs+++ b/core/src/smtp/outlook/microsoft365.rs@@ -19,7 +19,7 @@ use reqwest::Error as ReqwestError;use serde::Serialize;use crate::{- smtp::{http_api::create_client, SmtpDetails, VerifMethod},+ smtp::{http_api::create_client, SmtpDetails},util::ser_with_display::ser_with_display,CheckEmailInput, LOG_TARGET,};@@ -85,7 +85,6 @@ pub async fn check_microsoft365_api(Ok(Some(SmtpDetails {can_connect_smtp: true,is_deliverable: true,- verif_method: VerifMethod::Api,..Default::default()}))} else {diff --git a/core/src/smtp/yahoo/api.rs b/core/src/smtp/yahoo/api.rsindex 6f8c85fe..9801b513 100644--- a/core/src/smtp/yahoo/api.rs+++ b/core/src/smtp/yahoo/api.rs@@ -16,7 +16,7 @@use super::YahooError;use crate::{- smtp::{http_api::create_client, SmtpDetails, VerifMethod},+ smtp::{http_api::create_client, SmtpDetails},util::{constants::LOG_TARGET, input_output::CheckEmailInput},};use regex::Regex;@@ -168,7 +168,6 @@ pub async fn check_api(to_email: &str, input: &CheckEmailInput) -> Result<SmtpDeOk(SmtpDetails {can_connect_smtp: true,is_deliverable: username_exists,- verif_method: VerifMethod::Api,..Default::default()})}diff --git a/core/src/smtp/yahoo/headless.rs b/core/src/smtp/yahoo/headless.rsindex 25c864c0..26c3b19f 100644--- a/core/src/smtp/yahoo/headless.rs+++ b/core/src/smtp/yahoo/headless.rs@@ -20,10 +20,7 @@ use async_std::prelude::FutureExt;use fantoccini::Locator;use futures::TryFutureExt;-use crate::smtp::{- headless::{create_headless_client, HeadlessError},- VerifMethod,-};+use crate::smtp::headless::{create_headless_client, HeadlessError};use crate::{smtp::SmtpDetails, LOG_TARGET};/// Check if a Hotmail/Outlook email exists by connecting to the password@@ -114,7 +111,6 @@ pub async fn check_headless(to_email: &str, webdriver: &str) -> Result<SmtpDetaiis_catch_all: false,is_deliverable,is_disabled,- verif_method: VerifMethod::Headless,})}diff --git a/core/src/util/input_output.rs b/core/src/util/input_output.rsindex c5c854ae..46b78693 100644--- a/core/src/util/input_output.rs+++ b/core/src/util/input_output.rs@@ -24,7 +24,7 @@ use serde::{ser::SerializeMap, Deserialize, Serialize, Serializer};use crate::misc::{MiscDetails, MiscError};use crate::mx::{MxDetails, MxError};-use crate::smtp::{SmtpDetails, SmtpError, SmtpErrorDesc};+use crate::smtp::{SmtpDebug, SmtpDetails, SmtpError, SmtpErrorDesc};use crate::syntax::SyntaxDetails;/// Perform the email verification via a specified proxy. The usage of a proxy@@ -438,6 +438,8 @@ pub struct DebugDetails {pub end_time: DateTime<Utc>,/// The duration of the email verification.pub duration: Duration,+ /// Details about the email verification used for debugging.+ pub smtp: SmtpDebug,}impl Default for DebugDetails {@@ -447,6 +449,7 @@ impl Default for DebugDetails {start_time: SystemTime::now().into(),end_time: SystemTime::now().into(),duration: Duration::default(),+ smtp: SmtpDebug::default(),}}}