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
feat: Return Option<bool> instead of bool (#13)
Amaury Martiny2aef3458f694
diff --git a/CHANGELOG.md b/CHANGELOG.mdnew file mode 100644index 00000000..cb580cb0--- /dev/null+++ b/CHANGELOG.md@@ -0,0 +1,11 @@+# Change Log++All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.++# [0.1.1](https://github.com/amaurymartiny/check_if_email_exists/compare/v0.1.0...v0.1.1) (2018-12-29)++- Remove i686 arch ([36158fa](https://github.com/amaurymartiny/check_if_email_exists/commit/36158fa87415eaca143f281fc15244e8515ed579))++# 0.1.0 (2018-12-29)++- Change codebase to Rust ([#7](https://github.com/amaurymartiny/check_if_email_exists/pull/7)) ([05569e4](https://github.com/amaurymartiny/check_if_email_exists/commit/05569e4900b4467fa6d7f03086343fac753fe4ad))diff --git a/Cargo.lock b/Cargo.lockindex 3866d627..b5b5b0bb 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -1,3 +1,5 @@+# This file is automatically @generated by Cargo.+# It is not intended for manual editing.[[package]]name = "MacTypes-sys"version = "2.1.0"@@ -120,7 +122,7 @@ version = "0.1.1"dependencies = ["clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)","env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",- "lettre 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",+ "lettre 0.9.0 (git+https://github.com/lettre/lettre?rev=1cbcbbb11f7a39a5d5d3829ce4162ca1beafedfb)","log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)","native-tls 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)","rayon 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",@@ -407,12 +409,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"[[package]]name = "lettre"version = "0.9.0"-source = "registry+https://github.com/rust-lang/crates.io-index"+source = "git+https://github.com/lettre/lettre?rev=1cbcbbb11f7a39a5d5d3829ce4162ca1beafedfb#1cbcbbb11f7a39a5d5d3829ce4162ca1beafedfb"dependencies = ["base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)","bufstream 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",- "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",- "failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)","fast_chemail 0.9.6 (registry+https://github.com/rust-lang/crates.io-index)","hostname 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)","log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",@@ -1439,7 +1439,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index""checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d""checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14""checksum lazycell 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f"-"checksum lettre 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "dce706506538f9ed237258b11e3e04b39a1d49b5c8a085b96a3990d14f4cc5d4"+"checksum lettre 0.9.0 (git+https://github.com/lettre/lettre?rev=1cbcbbb11f7a39a5d5d3829ce4162ca1beafedfb)" = "<none>""checksum libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)" = "aab692d7759f5cd8c859e169db98ae5b52c924add2af5fbbca11d12fefb567c1""checksum linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ae91b68aebc4ddb91978b11a1b02ddd8602a05ec19002801c5666000e05e0f83""checksum lock_api 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "62ebf1391f6acad60e5c8b43706dde4582df75c06698ab44511d15016bc2442c"diff --git a/Cargo.toml b/Cargo.tomlindex 3b4e6e26..5dd02958 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -14,7 +14,7 @@ doc = false[dependencies]clap = {version = "2.32", features = ["yaml"]}env_logger = "0.6"-lettre = "0.9"+lettre = { git = "https://github.com/lettre/lettre", rev = "1cbcbbb11f7a39a5d5d3829ce4162ca1beafedfb" }log = "0.4"native-tls = "^0.2"rayon = "1.0.3"diff --git a/src/bin/main.rs b/src/bin/main.rsindex 74947188..39115b61 100644--- a/src/bin/main.rs+++ b/src/bin/main.rs@@ -17,5 +17,8 @@ fn main() {// Calling .unwrap() is safe here because "TO" is requiredlet to_email = matches.value_of("TO_EMAIL").unwrap();- println!("{}", email_exists(from_email, to_email));+ match email_exists(from_email, to_email) {+ Some(value) => println!("{}", value),+ None => println!("Can't check if email exists, see logs for more info"),+ }}diff --git a/src/lib.rs b/src/lib.rsindex 30a299be..872543fe 100644--- a/src/lib.rs+++ b/src/lib.rs@@ -12,7 +12,7 @@ use std::process;mod mx_hosts;mod smtp;-pub fn email_exists(from_email: &str, to_email: &str) -> bool {+pub fn email_exists(from_email: &str, to_email: &str) -> Option<bool> {debug!("Checking email '{}'", to_email);let domain = match to_email.split("@").skip(1).next() {@@ -38,5 +38,6 @@ pub fn email_exists(from_email: &str, to_email: &str) -> bool {combinations// Parallely find any combination that returns true for email_exists.par_iter()- .any(|(host, port)| smtp::email_exists(from_email, to_email, host, *port).is_some())+ .flat_map(|(host, port)| smtp::email_exists(from_email, to_email, host, *port))+ .find_any(|_| true)}diff --git a/src/smtp.rs b/src/smtp.rsindex b7c1e313..439f7bf6 100644--- a/src/smtp.rs+++ b/src/smtp.rs@@ -1,6 +1,7 @@use lettre::smtp::client::net::NetworkStream;use lettre::smtp::client::InnerClient;use lettre::smtp::commands::*;+use lettre::smtp::error::Error;use lettre::smtp::extension::ClientId;use lettre::EmailAddress;use std::time::Duration;@@ -9,32 +10,34 @@ use trust_dns_resolver::Name;// Try to send an smtp command, close if fails.macro_rules! try_smtp (($res: expr, $client: ident, $host: expr, $port: expr) => ({- match $res {- Ok(res) => res,- Err(err) => {- debug!("Closing {}:{}, because of error '{}'.", $host, $port, err);- $client.close();- return None;- },- }+ if let Err(err) = $res {+ debug!("Closing {}:{}, because of error '{}'.", $host, $port, err);+ $client.close();+ return Err(err);+ }}));-pub fn email_exists(from_email: &str, to_email: &str, host: &Name, port: u16) -> Option<bool> {- debug!("Connecting to {}:{}...", host, port);+pub fn email_exists(+ from_email: &str,+ to_email: &str,+ host: &Name,+ port: u16,+) -> Result<bool, Error> {+ debug!("Connecting to {}:{}", host, port);let mut smtp_client: InnerClient<NetworkStream> = InnerClient::new();+ let timeout = Some(Duration::new(3, 0)); // Set timeout to 3s// Set timeout.- try_smtp!(- smtp_client.set_timeout(Some(Duration::new(3, 0))),- smtp_client,- host,- port- );+ if let Err(err) = smtp_client.set_timeout(timeout) {+ debug!("Closing {}:{}, because of error '{}'.", host, port, err);+ smtp_client.close();+ return Err(Error::from(err));+ }// Connect to the host.try_smtp!(- smtp_client.connect(&(host.to_utf8().as_str(), port), None),+ smtp_client.connect(&(host.to_utf8().as_str(), port), timeout, None),smtp_client,host,port@@ -68,19 +71,19 @@ pub fn email_exists(from_email: &str, to_email: &str, host: &Name, port: u16) ->Some(message) => {// 250 2.1.5 Recipient e-mail address ok.if message.contains("2.1.5") {- Some(true)+ Ok(true)} else {- None+ Err(Error::Client("Can't find 2.1.5 in RCPT command"))}}- _ => None,+ None => Err(Error::Client("No response on RCPT command")),},Err(err) => {// 550 5.1.1 Mailbox does not exist.if err.to_string().contains("5.1.1") {- Some(false)+ Ok(false)} else {- None+ Err(err)}}};@@ -89,8 +92,8 @@ pub fn email_exists(from_email: &str, to_email: &str, host: &Name, port: u16) ->smtp_client.close();match result {- Some(val) => debug!("Checked email on {}:{}, exists={}.", host, port, val),- None => debug!("Cannot check email on {}:{}.", host, port),+ Ok(val) => debug!("Checked email on {}:{}, exists={}.", host, port, val),+ Err(_) => debug!("Cannot check email on {}:{}.", host, port),};result}