Skip to content

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

Separate library from binary

Amaury Martinyb92fa48c2bcd
diff --git a/Cargo.lock b/Cargo.lock
index c522cfd1..f179a878 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -116,7 +116,7 @@ version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
-name = "check-if-email-exists"
+name = "check_if_email_exists"
version = "0.1.0"
dependencies = [
"clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/Cargo.toml b/Cargo.toml
index 7173b0ba..75cf4f91 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,8 +1,16 @@
[package]
-name = "check-if-email-exists"
+name = "check_if_email_exists"
version = "0.1.0"
authors = ["Amaury Martiny <[email protected]>"]
+[lib]
+name = "check_if_email_exists"
+path = "src/lib.rs"
+
+[[bin]]
+name = "check_if_email_exists_bin"
+path = "src/bin/main.rs"
+
[dependencies]
clap = {version = "2.32", features = ["yaml"]}
env_logger = "0.5.13"
diff --git a/src/cli.yml b/src/bin/cli.yml
similarity index 100%
rename from src/cli.yml
rename to src/bin/cli.yml
diff --git a/src/bin/main.rs b/src/bin/main.rs
new file mode 100644
index 00000000..28164355
--- /dev/null
+++ b/src/bin/main.rs
@@ -0,0 +1,22 @@
+#[macro_use]
+extern crate clap;
+extern crate check_if_email_exists;
+extern crate lettre;
+extern crate native_tls;
+extern crate rayon;
+extern crate trust_dns_resolver;
+
+use check_if_email_exists::email_exists;
+use clap::App;
+
+fn main() {
+ // The YAML file is found relative to the current file, similar to how modules are found
+ let yaml = load_yaml!("cli.yml");
+ let matches = App::from_yaml(yaml).get_matches();
+
+ let from_email = matches.value_of("from").unwrap_or("[email protected]");
+ // Calling .unwrap() is safe here because "TO" is required
+ let to_email = matches.value_of("TO").unwrap();
+
+ println!("{}", email_exists(from_email, to_email));
+}
diff --git a/src/main.rs b/src/lib.rs
similarity index 66%
rename from src/main.rs
rename to src/lib.rs
index bf5fddfc..ecea9a29 100644
--- a/src/main.rs
+++ b/src/lib.rs
@@ -1,5 +1,3 @@
-#[macro_use]
-extern crate clap;
extern crate env_logger;
extern crate lettre;
#[macro_use]
@@ -8,7 +6,6 @@ extern crate native_tls;
extern crate rayon;
extern crate trust_dns_resolver;
-use clap::App;
use lettre::smtp::{SMTP_PORT, SUBMISSIONS_PORT, SUBMISSION_PORT};
use rayon::prelude::*;
use std::process;
@@ -16,18 +13,8 @@ use std::process;
mod mx_hosts;
mod smtp;
-fn main() {
- env_logger::init();
-
- // The YAML file is found relative to the current file, similar to how modules are found
- let yaml = load_yaml!("cli.yml");
- let matches = App::from_yaml(yaml).get_matches();
-
- let from_email = matches.value_of("from").unwrap_or("[email protected]");
- // Calling .unwrap() is safe here because "TO" is required
- let to_email = matches.value_of("TO").unwrap();
-
- debug!("User inputted email '{}'", to_email);
+pub fn email_exists(from_email: &str, to_email: &str) -> bool {
+ debug!("Checking email '{}'", to_email);
let domain = match to_email.split("@").skip(1).next() {
Some(i) => i,
@@ -49,7 +36,7 @@ fn main() {
}
}
- let found = combinations
+ combinations
.par_iter() // Parallelize the find_any
.find_any(
|(host, port)| match smtp::email_exists(from_email, to_email, host, *port) {
@@ -59,7 +46,5 @@ fn main() {
}
_ => false,
},
- ).is_some();
-
- println!("{}", found);
+ ).is_some()
}