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: Add Dockerfile & `x86_64-unknown-linux-musl` target (#86)
Amaury Martinycba124110be0
diff --git a/.dockerignore b/.dockerignorenew file mode 100644index 00000000..5504c638--- /dev/null+++ b/.dockerignore@@ -0,0 +1,5 @@+.git+.github+ci+target+test_suitediff --git a/.travis.yml b/.travis.ymlindex 7914dd80..2c04f05e 100644--- a/.travis.yml+++ b/.travis.yml@@ -52,7 +52,7 @@ matrix:# - env: TARGET=powerpc64le-unknown-linux-gnu# - env: TARGET=s390x-unknown-linux-gnu DISABLE_TESTS=1- env: TARGET=x86_64-unknown-linux-gnu- # - env: TARGET=x86_64-unknown-linux-musl+ - env: TARGET=x86_64-unknown-linux-musl# OSX# - env: TARGET=i686-apple-darwindiff --git a/CHANGELOG.md b/CHANGELOG.mdindex e7604a37..2cd0f8ab 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -2,8 +2,6 @@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.5.1](https://github.com/amaurymartiny/check_if_email_exists/compare/v0.5.0...v0.5.1) (2019-11-20)-# [0.5.0](https://github.com/amaurymartiny/check_if_email_exists/compare/v0.4.0...v0.5.0) (2019-11-16)diff --git a/Cargo.lock b/Cargo.lockindex 5b1f1fa4..6e6229b8 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -118,7 +118,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"[[package]]name = "check-if-email-exists"-version = "0.5.0"+version = "0.6.0"dependencies = ["futures 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)","lettre 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)",@@ -133,9 +133,9 @@ dependencies = [[[package]]name = "check-if-email-exists-cli"-version = "0.5.0"+version = "0.6.0"dependencies = [- "check-if-email-exists 0.5.0",+ "check-if-email-exists 0.6.0","clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)","env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)","futures-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)",@@ -147,9 +147,9 @@ dependencies = [[[package]]name = "check-if-email-exists-test-suite"-version = "0.5.0"+version = "0.6.0"dependencies = [- "check-if-email-exists 0.5.0",+ "check-if-email-exists 0.6.0","futures 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)","serde 1.0.103 (registry+https://github.com/rust-lang/crates.io-index)","serde_json 1.0.42 (registry+https://github.com/rust-lang/crates.io-index)",diff --git a/Cargo.toml b/Cargo.tomlindex 80996153..7d76eef8 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -1,6 +1,26 @@+[package]+name = "check-if-email-exists-cli"+version = "0.6.0"+default-run = "check_if_email_exists"+edition = "2018"+publish = false++[[bin]]+name = "check_if_email_exists"+path = "src/main.rs"++[dependencies]+check-if-email-exists = { path = "./core" }+clap = { version = "2.32", features = ["yaml"] }+env_logger = "0.7"+futures-preview = '0.3.0-alpha.19'+hyper = { version = "0.13.0-alpha.4", features = ["unstable-stream"] }+serde = "1.0"+serde_json = "1.0"+tokio = "0.2.0-alpha.6"+[workspace]members = [- "cli","core","test_suite"]diff --git a/Dockerfile b/Dockerfilenew file mode 100644index 00000000..d727c4bb--- /dev/null+++ b/Dockerfile@@ -0,0 +1,17 @@+FROM ubuntu++# `ciee` stands for check-if-email-exists+WORKDIR /ciee+ENV CIEE_VERSION 0.6.0++# Install needed libraries+RUN apt-get update && \+ apt-get install -y wget++# Download the binary from Github+RUN wget https://github.com/amaurymartiny/check-if-email-exists/releases/download/v${CIEE_VERSION}/check-if-email-exists-v${CIEE_VERSION}-x86_64-unknown-linux-gnu.tar.gz \+ && tar -xvzf check-if-email-exists-v${CIEE_VERSION}-x86_64-unknown-linux-gnu.tar.gz++RUN ls -la++CMD ["./check_if_email_exists", "--http"]diff --git a/README.md b/README.mdindex e91a819a..1419d064 100644--- a/README.md+++ b/README.md@@ -1,52 +1,172 @@-# check-if-email-exists--Check if an email address exists before sending the email.-[](https://crates.io/crates/check-if-email-exists)-[](https://travis-ci.org/amaurymartiny/check-if-email-exists)-[](https://ci.appveyor.com/project/amaurymartiny/check-if-email-exists-a08kp)+[](https://docs.rs/check-if-email-exists)+[](https://travis-ci.org/amaurymartiny/check-if-email-exists)+[](https://ci.appveyor.com/project/amaurymartiny/check-if-email-exists-a08kp)-[](https://offset.earth/amaurymartiny)+[](https://offset.earth/amaurymartiny)++<br /><br /><br />++<h1 align="center">check-if-email-exists</h1>+<h4 align="center">Check if an email address exists before sending the email.</h4>++<br /><br /><br />#### 👉 Try it here: https://3nbyey1wsi.execute-api.us-east-1.amazonaws.com/dev/?to_email=YOUR_EMAIL_HEREAnd replace the `YOUR_EMAIL_HERE` placeholder with the email you would like to verify.-> Note: The above operation might take up to 1 minute.+> Note: The above operation might take up to 15s.++## ✅ What Does This Tool Check?++The main feature this tool checks is:++✅ **Email deliverability:** Is an email for this address deliverable?++However, it goes more into details, and checks all the following properties of an email address:++✔️ **Syntax validation.** Is the address syntactically valid?++✔️ **DNS records validation.** Does the domain of the email address have valid MX DNS records?++✔️ **Disposable email address (DEA) validation.** Is the address provided by a known [disposable email address](https://en.wikipedia.org/wiki/Disposable_email_address) provider?++✔️ **SMTP server validation.** Can the mail exchanger of the email address domain be contacted successfully?-## Why?+✔️ **Mailbox disabled.** Has this email address been disabled by the email provider?++✔️ **Full inbox.** Is the inbox of this mailbox full?++✔️ **Catch-all address.** Is this email address a [catch-all](https://debounce.io/blog/help/what-is-a-catch-all-or-accept-all/) address?++Planned features:++- [ ] **Role account validation.** Is the email address a well-known role account?+- [ ] **Free email provider check.** Is the email address bound to a known free email provider?+- [ ] **Syntax validation, provider-specific.** According to the syntactic rules of the target mail provider, is the address syntactically valid?+- [ ] **Honeypot detection.** Does email address under test hide a [honeypot](https://en.wikipedia.org/wiki/Spamtrap)?+- [ ] **Gravatar.** Does this email address have a [Gravatar](https://gravatar.com/) profile picture?++## 🤔 Why?Many online services (https://hunter.io, http://verify-email.org, http://email-checker.net) offer this service for a paid fee. Here is an open-source alternative to those tools.-## Download the binary+## 🚀 Try It Yourself++There are 4 ways you can try `check-if-email-exists`.-Head to the [releases page](https://github.com/amaurymartiny/check-if-email-exists/releases) and download the binary for your platform.+### 1. Use the Hosted Version-> Note: The binary doesn't connect to the above `now.sh` backend, it checks the mail directly from your computer.+Try it here: https://3nbyey1wsi.execute-api.us-east-1.amazonaws.com/dev/?to_email=YOUR_EMAIL_HERE-## CLI Usage+And replace the `YOUR_EMAIL_HERE` placeholder with the email you would like to verify.++> Note: The above operation might take up to 15s.++### 2. Use Docker-Make sure you have [`openssl`](https://www.openssl.org/) installed.+The [Docker image](./Dockerfile) is hosted on Docker Hub: https://hub.docker.com/r/amaurymartiny/check-if-email-exists.+To run it, run the following command:++```bash+docker run -p 3000:3000 amaurymartiny/check-if-email-exists```++You can then send a POST request with the following body (`from_email` is optional) to `http://localhost:3000`:++```json+{+ "from_email": "[email protected]",+ "to_email": "[email protected]"+}+```++Here's the equivalent `curl` command:++```bash+curl -X POST -d'{"from_email":"[email protected]","to_email":"[email protected]"}' http://localhost:3000+```++### 3. Download the Binary++> Note: The binary doesn't connect to the above `amazonaws.com` backend, it checks the mail directly from your computer.++Head to the [releases page](https://github.com/amaurymartiny/check-if-email-exists/releases) and download the binary for your platform. Make sure you have [`openssl`](https://www.openssl.org/) installed on your local machine.++```+> $ check_if_email_exists --help+Check if an email address exists without sending any email.+USAGE:- check_if_email_exists [OPTIONS] <TO_EMAIL>+ check_if_email_exists [FLAGS] [OPTIONS] [TO_EMAIL]FLAGS:+ --http Runs a HTTP server-h, --help Prints help information-V, --version Prints version informationOPTIONS:--from <FROM_EMAIL> The from email to use in the SMTP connection [default: [email protected]]+ --http-port <PORT> Sets the port on which the HTTP server should bind. Only used when `--http` flag is on+ [default: 3000]ARGS:<TO_EMAIL> The email to check```-The output will be a JSON with the following format, for `[email protected]` (note that it is disabled by Gmail):+If you run with the `--http` flag, `check-if-email-exists` will serve a HTTP server on `http://localhost:3000`. You can then send a POST request with the following body (`from_email` is optional):```json{+ "from_email": "[email protected]",+ "to_email": "[email protected]"+}+```++Here's the equivalent `curl` command:++```bash+curl -X POST -d'{"from_email":"[email protected]","to_email":"[email protected]"}' http://localhost:3000+```++**💡 PRO TIP:** To show debug logs when running the binary, run:++```bash+RUST_LOG=debug check_if_email_exists [FLAGS] [OPTIONS] [TO_EMAIL]+```++### 4. Usage as a Library (Advanced)++In your own Rust project, you can add `check-if-email-exists` in your `Cargo.toml`:++```toml+[dependencies]+check-if-email-exists = "0.6"+```++And use it in your code as follows (async/await syntax):++```rust++use check_if_email_exists::email_exists;++// First arg is the email we want to check, second arg is the FROM email used in the SMTP connection+let checked = email_exists("[email protected]", "[email protected]").await;++println!({:?}, checked); // `checked` is a SingleEmail struct, see docs for more info+```++The reference docs are hosted on [docs.rs](https://docs.rs/check-if-email-exists).++## ✈️ JSON Output++The output will be a JSON with the below format, the fields should be self-explanatory. For `[email protected]` (note that it is disabled by Gmail), here's the exact output:++```json+{+ "input": "[email protected]","mx": {"is_disposable": false,"records": [@@ -72,36 +192,7 @@ The output will be a JSON with the following format, for `[email protected]` (no}```-### Verbose Mode--To show debug logs when running the binary, run:--```bash-RUST_LOG=debug check_if_email_exists [OPTIONS] <TO_EMAIL>-```--## Usage as a Library--In your own Rust project, you can add `check-if-email-exists` in your `Cargo.toml`:--```toml-[dependencies]-check-if-email-exists = "0.4"-```--And use it in your code as follows:--```rust--use check_if_email_exists::email_exists;--// First arg is the email we want to check, second arg is the FROM email used in the SMTP connection-let checked = email_exists("[email protected]", "[email protected]");--println!({:?}, checked); // `checked` is a SingleEmail struct-```--## FAQ+## ❓ FAQ### The library hangs/takes a long time/doesn't show anything after 1 minute.@@ -109,13 +200,13 @@ Most ISPs block outgoing SMTP requests through ports 25, 587 and 465, to preventTo see in details what the binary is doing behind the scenes, run it in [verbose mode](#verbose-mode) to see the logs.-## Legacy Bash Script+### The output shows `"connection refused"` in the `smtp` field.-The 1st version of this tool was a simple bash script which made a telnet call. If you would like to use that simpler version, have a look at the [`legacy`](https://github.com/amaurymartiny/check-if-email-exists/tree/legacy) branch. The reasons for porting the bash script to the current codebase are explained [in this issue](https://github.com/amaurymartiny/check-if-email-exists/issues/4).+This also happens when your ISP block SMTP ports, see the above answer.-## Build From Source+## 🔨 Build From Source-First, [install Rust](https://www.rust-lang.org/tools/install); you'll need Rust 1.37.0 or later. Then, clone the source code locally:+First, [install Rust](https://www.rust-lang.org/tools/install); you'll need Rust 1.37.0 or later. Then, run the following commands:```bash# Download the code@@ -126,9 +217,13 @@ $ cd check-if-email-exists$ cargo build --release# Run the binary-$ ./target/release/check-if-email-exists --help+$ ./target/release/check_if_email_exists --help```-## License+## 👣 Legacy Bash Script++The 1st version of this tool was a simple bash script which made a telnet call. If you would like to use that simpler version, have a look at the [`legacy`](https://github.com/amaurymartiny/check-if-email-exists/tree/legacy) branch. The reasons for porting the bash script to the current codebase are explained [in this issue](https://github.com/amaurymartiny/check-if-email-exists/issues/4).++## 📜 LicenseGPL-3.0. See the [LICENSE](./LICENSE) file for more info.diff --git a/cli/Cargo.toml b/cli/Cargo.tomldeleted file mode 100644index d483880f..00000000--- a/cli/Cargo.toml+++ /dev/null@@ -1,20 +0,0 @@-[package]-name = "check-if-email-exists-cli"-version = "0.5.0"-default-run = "check_if_email_exists"-edition = "2018"-publish = false--[[bin]]-name = "check_if_email_exists"-path = "src/main.rs"--[dependencies]-check-if-email-exists = { path = "../core" }-clap = { version = "2.32", features = ["yaml"] }-env_logger = "0.7"-futures-preview = '0.3.0-alpha.19'-hyper = { version = "0.13.0-alpha.4", features = ["unstable-stream"] }-serde = "1.0"-serde_json = "1.0"-tokio = "0.2.0-alpha.6"diff --git a/core/Cargo.toml b/core/Cargo.tomlindex c197c5bb..d0a883d3 100644--- a/core/Cargo.toml+++ b/core/Cargo.toml@@ -1,6 +1,6 @@[package]name = "check-if-email-exists"-version = "0.5.0"+version = "0.6.0"authors = ["Amaury Martiny <[email protected]>"]categories = ["email"]description = "Check if an email address exists without sending any email"diff --git a/core/src/lib.rs b/core/src/lib.rsindex 8015bdf4..07dc56e2 100644--- a/core/src/lib.rs+++ b/core/src/lib.rs@@ -23,13 +23,15 @@ extern crate rand;extern crate serde;extern crate trust_dns_resolver;-mod mx;-mod smtp;-mod syntax;+pub mod misc;+pub mod mx;+pub mod smtp;+pub mod syntax;mod util;use futures::future::select_ok;use lettre::{smtp::SMTP_PORT, EmailAddress};+use misc::{misc_details, MiscDetails, MiscError};use mx::{get_mx_lookup, MxDetails, MxError};use serde::{ser::SerializeMap, Serialize, Serializer};use smtp::{SmtpDetails, SmtpError};@@ -41,6 +43,8 @@ use syntax::{address_syntax, SyntaxDetails, SyntaxError};pub struct SingleEmail {/// Input by the userpub input: String,+ /// Misc details about the email address+ pub misc: Result<MiscDetails, MiscError>,/// Details about the MX hostpub mx: Result<MxDetails, MxError>,/// Details about the SMTP responses of the email@@ -63,6 +67,10 @@ impl Serialize for SingleEmail {let mut map = serializer.serialize_map(Some(1))?;map.serialize_entry("input", &self.input)?;+ match &self.misc {+ Ok(t) => map.serialize_entry("misc", &t)?,+ Err(error) => map.serialize_entry("misc", &MyError { error })?,+ }match &self.mx {Ok(t) => map.serialize_entry("mx", &t)?,Err(error) => map.serialize_entry("mx", &MyError { error })?,@@ -92,6 +100,7 @@ pub async fn email_exists(to_email: &str, from_email: &str) -> SingleEmail {e => {return SingleEmail {input: to_email.into(),+ misc: Err(MiscError::Skipped),mx: Err(MxError::Skipped),smtp: Err(SmtpError::Skipped),syntax: e,@@ -106,6 +115,7 @@ pub async fn email_exists(to_email: &str, from_email: &str) -> SingleEmail {e => {return SingleEmail {input: to_email.into(),+ misc: Err(MiscError::Skipped),mx: e,smtp: Err(SmtpError::Skipped),syntax: Ok(my_syntax),@@ -114,6 +124,9 @@ pub async fn email_exists(to_email: &str, from_email: &str) -> SingleEmail {};debug!("Found the following MX hosts {:?}", my_mx);+ debug!("Getting misc details...");+ let my_misc = misc_details(&my_syntax);+// Create one future per lookup resultlet futures = my_mx.lookup@@ -141,6 +154,7 @@ pub async fn email_exists(to_email: &str, from_email: &str) -> SingleEmail {SingleEmail {input: to_email.into(),+ misc: Ok(my_misc),mx: Ok(my_mx),smtp: my_smtp,syntax: Ok(my_syntax),diff --git a/core/src/misc.rs b/core/src/misc.rsnew file mode 100644index 00000000..e604d626--- /dev/null+++ b/core/src/misc.rs@@ -0,0 +1,44 @@+// check-if-email-exists+// Copyright (C) 2018-2019 Amaury Martiny++// check-if-email-exists is free software: you can redistribute it and/or modify+// it under the terms of the GNU General Public License as published by+// the Free Software Foundation, either version 3 of the License, or+// (at your option) any later version.++// check-if-email-exists is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU General Public License for more details.++// You should have received a copy of the GNU General Public License+// along with check-if-email-exists. If not, see <http://www.gnu.org/licenses/>.++use crate::syntax::SyntaxDetails;+use mailchecker;+use serde::Serialize;++/// Details that we gathered from connecting to this email via SMTP+#[derive(Debug, Serialize)]+pub struct MiscDetails {+ /// Is this a DEA (disposable email account)?+ pub is_disposable: bool,+}++/// Error occured connecting to this email server via SMTP+#[derive(Debug, Serialize)]+#[serde(tag = "type", content = "message")]+pub enum MiscError {+ /// Skipped checking SMTP details+ Skipped,+}++/// Fetch misc details about the email address, such as whether it's disposable+pub fn misc_details(syntax: &SyntaxDetails) -> MiscDetails {+ MiscDetails {+ // mailchecker::is_valid checks also if the syntax is valid. But if+ // we're here, it means we're sure the syntax is valid, so is_valid+ // actually will only check the disposable email Misc.+ is_disposable: !mailchecker::is_valid(syntax.address.to_string().as_ref()),+ }+}diff --git a/core/src/mx.rs b/core/src/mx.rsindex c3432f52..7656c317 100644--- a/core/src/mx.rs+++ b/core/src/mx.rs@@ -16,7 +16,6 @@use crate::syntax::SyntaxDetails;use crate::util::ser_with_display;-use mailchecker;use serde::{ser::SerializeMap, Serialize, Serializer};use std::io::Error;use trust_dns_resolver::config::*;@@ -27,7 +26,7 @@ use trust_dns_resolver::Resolver;/// Details about the MX lookup#[derive(Debug)]pub struct MxDetails {- pub is_disposable: bool,+ /// MX lookup of this DNSpub lookup: MxLookup,}@@ -45,7 +44,6 @@ impl Serialize for MxDetails {.map(|host| host.exchange().to_string()).collect::<Vec<_>>(),)?;- map.serialize_entry("is_disposable", &self.is_disposable)?;map.end()}}@@ -78,13 +76,7 @@ pub fn get_mx_lookup(syntax: &SyntaxDetails) -> Result<MxDetails, MxError> {// The final dot forces this to be an FQDN, otherwise the search rules as specified// in `ResolverOpts` will take effect. FQDN's are generally cheaper queries.match resolver.mx_lookup(syntax.domain.as_ref()) {- Ok(lookup) => Ok(MxDetails {- // mailchecker::is_valid checks also if the syntax is valid. But if- // we're here, it means we're sure the syntax is valid, so is_valid- // actually will only check the disposable email provider.- is_disposable: !mailchecker::is_valid(syntax.address.to_string().as_ref()),- lookup,- }),+ Ok(lookup) => Ok(MxDetails { lookup }),Err(err) => Err(MxError::ResolveError(err)),}}diff --git a/cli/src/cli.yml b/src/cli.ymlsimilarity index 97%rename from cli/src/cli.ymlrename to src/cli.ymlindex c7a94996..8882cb0f 100644--- a/cli/src/cli.yml+++ b/src/cli.yml@@ -1,5 +1,5 @@name: check_if_email_exists-version: "0.5.0"+version: "0.6.0"author: Amaury Martiny <[email protected]>about: Check if an email address exists without sending any email.args:diff --git a/cli/src/http.rs b/src/http.rssimilarity index 100%rename from cli/src/http.rsrename to src/http.rsdiff --git a/cli/src/main.rs b/src/main.rssimilarity index 100%rename from cli/src/main.rsrename to src/main.rsdiff --git a/test_suite/Cargo.toml b/test_suite/Cargo.tomlindex cb4420fb..461bb440 100644--- a/test_suite/Cargo.toml+++ b/test_suite/Cargo.toml@@ -1,6 +1,6 @@[package]name = "check-if-email-exists-test-suite"-version = "0.5.0"+version = "0.6.0"edition = "2018"publish = falsediff --git a/test_suite/src/lib.rs b/test_suite/src/lib.rsindex 1fe1ae7b..0b9bfb75 100644--- a/test_suite/src/lib.rs+++ b/test_suite/src/lib.rs@@ -26,7 +26,7 @@ mod tests {let result = block_on(email_exists("foo", "[email protected]"));assert_eq!(serde_json::to_string(&result).unwrap(),- "{\"input\":\"foo\",\"mx\":{\"error\":{\"type\":\"Skipped\"}},\"smtp\":{\"error\":{\"type\":\"Skipped\"}},\"syntax\":{\"error\":{\"type\":\"SyntaxError\",\"message\":\"invalid email address\"}}}"+ "{\"input\":\"foo\",\"misc\":{\"error\":{\"type\":\"Skipped\"}},\"mx\":{\"error\":{\"type\":\"Skipped\"}},\"smtp\":{\"error\":{\"type\":\"Skipped\"}},\"syntax\":{\"error\":{\"type\":\"SyntaxError\",\"message\":\"invalid email address\"}}}");}@@ -36,7 +36,7 @@ mod tests {assert_eq!(serde_json::to_string(&result).unwrap(),- "{\"input\":\"[email protected]\",\"mx\":{\"error\":{\"type\":\"ResolveError\",\"message\":\"no record found for name: bar.baz type: MX class: IN\"}},\"smtp\":{\"error\":{\"type\":\"Skipped\"}},\"syntax\":{\"address\":\"[email protected]\",\"domain\":\"bar.baz\",\"username\":\"foo\",\"valid_format\":true}}"+ "{\"input\":\"[email protected]\",\"misc\":{\"error\":{\"type\":\"Skipped\"}},\"mx\":{\"error\":{\"type\":\"ResolveError\",\"message\":\"no record found for name: bar.baz type: MX class: IN\"}},\"smtp\":{\"error\":{\"type\":\"Skipped\"}},\"syntax\":{\"address\":\"[email protected]\",\"domain\":\"bar.baz\",\"username\":\"foo\",\"valid_format\":true}}");}}