Skip to content

Check if an email address exists without sending any email, written in Rust. Comes with a ⚙️ HTTP backend.

feat: Add suggestions for syntax errors (#1192)

Daniel Huth2d385f30f7a6
diff --git a/Cargo.lock b/Cargo.lock
index a32b4045..fd2dea28 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -414,6 +414,7 @@ dependencies = [
"fantoccini",
"fast-socks5",
"futures",
+ "levenshtein",
"log",
"mailchecker",
"md5",
@@ -1264,6 +1265,12 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+[[package]]
+name = "levenshtein"
+version = "1.0.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "db13adb97ab515a3691f56e4dbab09283d0b86cb45abd991d8634a9d6f501760"
+
[[package]]
name = "libc"
version = "0.2.131"
diff --git a/README.md b/README.md
index c52422d3..1375bbf5 100644
--- a/README.md
+++ b/README.md
@@ -127,7 +127,8 @@ The output will be a JSON with the below format, the fields should be self-expla
"syntax": {
"domain": "gmail.com",
"is_valid_syntax": true,
- "username": "someone"
+ "username": "someone",
+ "suggestion": null
}
}
```
diff --git a/backend/tests/check_email.rs b/backend/tests/check_email.rs
index 99e1089e..d5318e61 100644
--- a/backend/tests/check_email.rs
+++ b/backend/tests/check_email.rs
@@ -22,8 +22,8 @@ use serde_json;
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},"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":""}}"#;
-const FOO_BAR_BAZ_RESPONSE: &str = r#"{"input":"[email protected]","is_reachable":"invalid","misc":{"is_disposable":false,"is_role_account":false,"gravatar_url":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"}}"#;
+const FOO_BAR_RESPONSE: &str = r#"{"input":"foo@bar","is_reachable":"invalid","misc":{"is_disposable":false,"is_role_account":false,"gravatar_url":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":"","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},"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","suggestion":null}}"#;
#[tokio::test]
async fn test_input_foo_bar() {
diff --git a/core/Cargo.toml b/core/Cargo.toml
index 0a564e91..4119d957 100644
--- a/core/Cargo.toml
+++ b/core/Cargo.toml
@@ -30,6 +30,7 @@ serde = { version = "1.0.145", features = ["derive"] }
serde_json = "1.0.85"
trust-dns-proto = "0.21.2"
md5 = "0.7.0"
+levenshtein = "1.0.5"
[dev-dependencies]
tokio = { version = "1.21.2" }
diff --git a/core/src/lib.rs b/core/src/lib.rs
index 5b988048..b7776bc6 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -72,7 +72,7 @@ mod util;
use misc::{check_misc, MiscDetails};
use mx::check_mx;
use smtp::{check_smtp, SmtpDetails, SmtpError};
-use syntax::check_syntax;
+use syntax::{check_syntax, get_similar_mail_provider};
pub use util::constants::LOG_TARGET;
pub use util::input_output::*;
@@ -116,7 +116,7 @@ pub async fn check_email(input: &CheckEmailInput) -> CheckEmailOutput {
to_email,
to_email
);
- let my_syntax = check_syntax(to_email.as_ref());
+ let mut my_syntax = check_syntax(to_email.as_ref());
if !my_syntax.is_valid_syntax {
return CheckEmailOutput {
input: to_email.to_string(),
@@ -136,6 +136,8 @@ pub async fn check_email(input: &CheckEmailInput) -> CheckEmailOutput {
let my_mx = match check_mx(&my_syntax).await {
Ok(m) => m,
e => {
+ get_similar_mail_provider(&mut my_syntax);
+
// This happens when there's an internal error while checking MX
// records. Should happen fairly rarely.
return CheckEmailOutput {
@@ -150,6 +152,8 @@ pub async fn check_email(input: &CheckEmailInput) -> CheckEmailOutput {
// Return if we didn't find any MX records.
if my_mx.lookup.is_err() {
+ get_similar_mail_provider(&mut my_syntax);
+
return CheckEmailOutput {
input: to_email.to_string(),
is_reachable: Reachable::Invalid,
@@ -213,6 +217,9 @@ pub async fn check_email(input: &CheckEmailInput) -> CheckEmailOutput {
let my_smtp = my_smtp.expect(
"As long as lookup has at least 1 element (which we checked), my_smtp will be a Some. qed.",
);
+ if my_smtp.is_err() {
+ get_similar_mail_provider(&mut my_syntax);
+ }
CheckEmailOutput {
input: to_email.to_string(),
diff --git a/core/src/syntax.rs b/core/src/syntax.rs
index 8359c50e..e21ab37f 100644
--- a/core/src/syntax.rs
+++ b/core/src/syntax.rs
@@ -15,6 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use async_smtp::EmailAddress;
+use levenshtein::levenshtein;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
@@ -32,6 +33,7 @@ pub struct SyntaxDetails {
/// The username, before "@". It will be the empty string if the email
/// address if ill-formed.
pub username: String,
+ pub suggestion: Option<String>,
}
impl Default for SyntaxDetails {
@@ -41,6 +43,7 @@ impl Default for SyntaxDetails {
domain: "".into(),
is_valid_syntax: false,
username: "".into(),
+ suggestion: None,
}
}
}
@@ -58,6 +61,7 @@ pub fn check_syntax(email_address: &str) -> SyntaxDetails {
domain: "".into(),
is_valid_syntax: false,
username: "".into(),
+ suggestion: None,
};
}
}
@@ -67,6 +71,7 @@ pub fn check_syntax(email_address: &str) -> SyntaxDetails {
domain: "".into(),
is_valid_syntax: false,
username: "".into(),
+ suggestion: None,
}
}
};
@@ -87,6 +92,34 @@ pub fn check_syntax(email_address: &str) -> SyntaxDetails {
domain,
is_valid_syntax: true,
username,
+ suggestion: None,
+ }
+}
+
+const MAIL_PROVIDERS: &[&str] = &[
+ "gmail.com",
+ "yahoo.com",
+ "outlook.com",
+ "hotmail.com",
+ "protonmail.com",
+ "icloud.com",
+ "yandex.com",
+];
+// Supplies the syntax parameter with a suggestion that matches the mail domain best by levenshtein
+// distance.
+pub fn get_similar_mail_provider(syntax: &mut SyntaxDetails) {
+ for possible_provider in MAIL_PROVIDERS {
+ let distance = levenshtein(&syntax.domain, possible_provider);
+
+ if distance < 3 {
+ // Return full address
+ syntax.suggestion = Some(format!(
+ "{}@{}",
+ syntax.username,
+ String::from(*possible_provider),
+ ));
+ break;
+ }
}
}
@@ -103,6 +136,7 @@ mod tests {
domain: "".into(),
is_valid_syntax: false,
username: "".into(),
+ suggestion: None,
}
);
}
@@ -116,6 +150,7 @@ mod tests {
domain: "".into(),
is_valid_syntax: false,
username: "".into(),
+ suggestion: None,
}
);
}
@@ -129,7 +164,21 @@ mod tests {
domain: "bar.com".into(),
is_valid_syntax: true,
username: "foo".into(),
+ suggestion: None,
}
);
}
+
+ #[test]
+ fn should_suggest_a_correct_mail_if_similar() {
+ let mut syntax = SyntaxDetails {
+ address: Some(EmailAddress::new("[email protected]".into()).unwrap()),
+ domain: "gmali.com".into(),
+ is_valid_syntax: true,
+ username: "test".into(),
+ suggestion: None,
+ };
+ get_similar_mail_provider(&mut syntax);
+ assert_eq!(syntax.suggestion, Some("[email protected]".to_string()))
+ }
}
diff --git a/core/src/util/input_output.rs b/core/src/util/input_output.rs
index 9e668e3d..64c8b9c6 100644
--- a/core/src/util/input_output.rs
+++ b/core/src/util/input_output.rs
@@ -405,20 +405,20 @@ mod tests {
let res = dummy_response_with_message("blacklist");
let actual = serde_json::to_string(&res).unwrap();
// Make sure the `description` is present with IpBlacklisted.
- let expected = r#"{"input":"foo","is_reachable":"unknown","misc":{"is_disposable":false,"is_role_account":false,"gravatar_url":null},"mx":{"accepts_mail":false,"records":[]},"smtp":{"error":{"type":"SmtpError","message":"transient: blacklist"},"description":"IpBlacklisted"},"syntax":{"address":null,"domain":"","is_valid_syntax":false,"username":""}}"#;
+ let expected = r#"{"input":"foo","is_reachable":"unknown","misc":{"is_disposable":false,"is_role_account":false,"gravatar_url":null},"mx":{"accepts_mail":false,"records":[]},"smtp":{"error":{"type":"SmtpError","message":"transient: blacklist"},"description":"IpBlacklisted"},"syntax":{"address":null,"domain":"","is_valid_syntax":false,"username":"","suggestion":null}}"#;
assert_eq!(expected, actual);
let res =
dummy_response_with_message("Client host rejected: cannot find your reverse hostname");
let actual = serde_json::to_string(&res).unwrap();
// Make sure the `description` is present with NeedsRDNs.
- let expected = r#"{"input":"foo","is_reachable":"unknown","misc":{"is_disposable":false,"is_role_account":false,"gravatar_url":null},"mx":{"accepts_mail":false,"records":[]},"smtp":{"error":{"type":"SmtpError","message":"transient: Client host rejected: cannot find your reverse hostname"},"description":"NeedsRDNS"},"syntax":{"address":null,"domain":"","is_valid_syntax":false,"username":""}}"#;
+ let expected = r#"{"input":"foo","is_reachable":"unknown","misc":{"is_disposable":false,"is_role_account":false,"gravatar_url":null},"mx":{"accepts_mail":false,"records":[]},"smtp":{"error":{"type":"SmtpError","message":"transient: Client host rejected: cannot find your reverse hostname"},"description":"NeedsRDNS"},"syntax":{"address":null,"domain":"","is_valid_syntax":false,"username":"","suggestion":null}}"#;
assert_eq!(expected, actual);
let res = dummy_response_with_message("foobar");
let actual = serde_json::to_string(&res).unwrap();
// Make sure the `description` is NOT present.
- let expected = r#"{"input":"foo","is_reachable":"unknown","misc":{"is_disposable":false,"is_role_account":false,"gravatar_url":null},"mx":{"accepts_mail":false,"records":[]},"smtp":{"error":{"type":"SmtpError","message":"transient: foobar"}},"syntax":{"address":null,"domain":"","is_valid_syntax":false,"username":""}}"#;
+ let expected = r#"{"input":"foo","is_reachable":"unknown","misc":{"is_disposable":false,"is_role_account":false,"gravatar_url":null},"mx":{"accepts_mail":false,"records":[]},"smtp":{"error":{"type":"SmtpError","message":"transient: foobar"}},"syntax":{"address":null,"domain":"","is_valid_syntax":false,"username":"","suggestion":null}}"#;
assert_eq!(expected, actual);
}
}