Skip to content

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

feat: Add `is_reachable` top field (#350)

Amaury Martinye7abb17ef296
diff --git a/Cargo.lock b/Cargo.lock
index 6be51eed..e8d3d86b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -213,7 +213,7 @@ checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "check-if-email-exists"
-version = "0.8.1"
+version = "0.8.2"
dependencies = [
"amaurymartiny-async-smtp",
"fast-socks5",
@@ -228,7 +228,7 @@ dependencies = [
[[package]]
name = "check-if-email-exists-cli"
-version = "0.8.1"
+version = "0.8.2"
dependencies = [
"check-if-email-exists",
"clap",
@@ -241,7 +241,7 @@ dependencies = [
[[package]]
name = "check-if-email-exists-test-suite"
-version = "0.8.1"
+version = "0.8.2"
dependencies = [
"check-if-email-exists",
"serde",
diff --git a/Cargo.toml b/Cargo.toml
index 0032d94d..1e7c6106 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "check-if-email-exists-cli"
-version = "0.8.1"
+version = "0.8.2"
default-run = "check_if_email_exists"
edition = "2018"
license = "GPL-3.0"
diff --git a/Dockerfile b/Dockerfile
index 320af340..9e8a7151 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -3,7 +3,7 @@ FROM alpine
# `ciee` stands for check-if-email-exists
WORKDIR /ciee
# Fetch latest version
-ENV CIEE_VERSION 0.8.1
+ENV CIEE_VERSION 0.8.2
# Install needed libraries
RUN apk update && \
diff --git a/README.md b/README.md
index aaccc9bb..2ae5cba8 100644
--- a/README.md
+++ b/README.md
@@ -20,11 +20,12 @@ This software is licensed under the GPL-3.0 license, which forbids it being inte
| Included? | Feature | Description | JSON field |
| --------- | --------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- |
-| ✅ | **Email deliverability** | Is an email sent to this address deliverable? | `smtp.is_deliverable` |
+| ✅ | **Email reachability** | How confident are we in sending an email to this address? Can be one of Safe, Risky, Invalid or Unknown. | `is_reachable` |
| ✅ | **Syntax validation** | Is the address syntactically valid? | `syntax.is_valid_syntax` |
| ✅ | **DNS records validation** | Does the domain of the email address have valid MX DNS records? | `mx.accepts_mail` |
| ✅ | **Disposable email address (DEA) validation** | Is the address provided by a known [disposable email address](https://en.wikipedia.org/wiki/Disposable_email_address) provider? | `misc.is_disposable` |
| ✅ | **SMTP server validation** | Can the mail exchanger of the email address domain be contacted successfully? | `smtp.can_connect_smtp` |
+| ✅ | **Email deliverability** | Is an email sent to this address deliverable? | `smtp.is_deliverable` |
| ✅ | **Mailbox disabled** | Has this email address been disabled by the email provider? | `smtp.is_disabled` |
| ✅ | **Full inbox** | Is the inbox of this mailbox full? | `smtp.has_full_inbox` |
| ✅ | **Catch-all address** | Is this email address a [catch-all](https://debounce.io/blog/help/what-is-a-catch-all-or-accept-all/) address? | `smtp.is_catch_all` |
@@ -45,7 +46,7 @@ There are 4 ways you can try `check-if-email-exists`.
### 1. Use the Hosted Version: https://reacher.email
-I created a simple static site with this tool: https://reacher.email. The backend is hosted on [fly.io](https://fly.io), the free tier endpoint is rate-limited to prevent abuse. Also see [issue #155](https://github.com/amaurymartiny/check-if-email-exists/issues/155).
+This web app is also open-source, at https://github.com/reacherhq/.
If you would like to self-host it yourself and have questions, send me a message.
@@ -59,18 +60,18 @@ To run it, run the following command:
docker run -p 3000:3000 amaurymartiny/check-if-email-exists
```
-You can then send a POST request with the following body to `http://localhost:3000`:
+You can then send a POST request with the following body to `http://localhost:3000` to test multiple emails at once:
```json
{
- "to_email": "[email protected]"
+ "to_emails": ["[email protected]"]
}
```
Here's the equivalent `curl` command:
```bash
-curl -X POST -d'{"to_email":"[email protected]"}' http://localhost:3000
+curl -X POST -d'{"to_emails":["[email protected]"]}' http://localhost:3000
```
Optionally, you can also pass in `from_email` and `hello_name` fields into the JSON object, see the help message below to understand their meanings.
@@ -83,7 +84,7 @@ Head to the [releases page](https://github.com/amaurymartiny/check-if-email-exis
```
> $ check_if_email_exists --help
-check_if_email_exists 0.8.1
+check_if_email_exists 0.8.2
Check if an email address exists without sending any email.
USAGE:
@@ -109,18 +110,18 @@ ARGS:
<TO_EMAIL> The email to check.
```
-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:
+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 to test multiple emails at once:
```json
{
- "to_email": "[email protected]"
+ "to_emails": ["[email protected]"]
}
```
Here's the equivalent `curl` command:
```bash
-curl -X POST -d'{"to_email":"[email protected]"}' http://localhost:3000
+curl -X POST -d'{"to_emails":["[email protected]"]}' http://localhost:3000
```
Optionally, you can also pass in `from_email` and `hello_name` fields into the JSON object, see the help message above to understand their meanings.
@@ -173,6 +174,7 @@ The output will be a JSON with the below format, the fields should be self-expla
```json
{
"input": "[email protected]",
+ "is_reachable": "invalid",
"misc": {
"is_disposable": false,
"is_role_account": false
@@ -202,7 +204,7 @@ The output will be a JSON with the below format, the fields should be self-expla
}
```
-You can also take a look at the [OpenAPIv3 specification](https://stoplight.io/p/docs/gh/reacherhq/openapi/openapi.json/components/schemas/EmailResult?srn=gh/reacherhq/backend/openapi.json/components/schemas/EmailResult) of this JSON object.
+You can also take a look at the [OpenAPIv3 specification](https://reacher.email/docs#operation/post-check-email) of this JSON object.
## ❓ FAQ
diff --git a/core/Cargo.toml b/core/Cargo.toml
index 6abbd2d9..c4ff636f 100644
--- a/core/Cargo.toml
+++ b/core/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "check-if-email-exists"
-version = "0.8.1"
+version = "0.8.2"
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.rs
index 6c7bb2e2..d5310410 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -55,9 +55,6 @@
//! }
//! ```
-#[macro_use]
-extern crate log;
-
mod misc;
mod mx;
mod smtp;
@@ -66,15 +63,33 @@ mod util;
use async_smtp::{smtp::SMTP_PORT, EmailAddress};
use futures::future;
-use misc::check_misc;
+use misc::{check_misc, MiscDetails};
use mx::check_mx;
-use smtp::check_smtp;
+use smtp::{check_smtp, SmtpDetails, SmtpError};
use std::str::FromStr;
use syntax::check_syntax;
use util::constants::LOG_TARGET;
pub use util::input_output::*;
+/// Given an email's misc and smtp details, calculate an estimate of our
+/// confidence on how reachable the email is.
+fn calculate_reachable(misc: &MiscDetails, smtp: &Result<SmtpDetails, SmtpError>) -> Reachable {
+ if let Ok(smtp) = smtp {
+ if misc.is_disposable || misc.is_role_account || smtp.is_catch_all || smtp.has_full_inbox {
+ return Reachable::Risky;
+ }
+
+ if !smtp.is_deliverable || !smtp.can_connect_smtp || smtp.is_disabled {
+ return Reachable::Invalid;
+ }
+
+ Reachable::Safe
+ } else {
+ Reachable::Unknown
+ }
+}
+
/// Check a single emails. This assumes this `input.check_email` contains
/// exactly one element. If it contains more, elements other than the first
/// one will be ignored.
@@ -84,7 +99,7 @@ pub use util::input_output::*;
/// This function panics if `input.check_email` is empty.
async fn check_single_email(input: CheckEmailInput) -> CheckEmailOutput {
let from_email = EmailAddress::from_str(input.from_email.as_ref()).unwrap_or_else(|_| {
- warn!(
+ log::warn!(
"Inputted from_email \"{}\" is not a valid email, using \"[email protected]\" instead",
input.from_email
);
@@ -93,7 +108,7 @@ async fn check_single_email(input: CheckEmailInput) -> CheckEmailOutput {
let to_email = &input.to_emails[0];
- debug!(target: LOG_TARGET, "Checking email \"{}\"", to_email);
+ log::debug!(target: LOG_TARGET, "Checking email \"{}\"", to_email);
let my_syntax = check_syntax(to_email.as_ref());
if !my_syntax.is_valid_syntax {
return CheckEmailOutput {
@@ -103,31 +118,37 @@ async fn check_single_email(input: CheckEmailInput) -> CheckEmailOutput {
};
}
- debug!(
+ log::debug!(
target: LOG_TARGET,
- "Found the following syntax validation: {:?}", my_syntax
+ "Found the following syntax validation: {:?}",
+ my_syntax
);
let my_mx = match check_mx(&my_syntax).await {
Ok(m) => m,
e => {
+ // This happens when there's an internal error while checking MX
+ // records.
return CheckEmailOutput {
input: to_email.to_string(),
+ is_reachable: Reachable::Unknown,
mx: e,
syntax: my_syntax,
..Default::default()
- }
+ };
}
};
- debug!(
+ log::debug!(
target: LOG_TARGET,
- "Found the following MX hosts {:?}", my_mx
+ "Found the following MX hosts {:?}",
+ my_mx
);
// Return if we didn't find any MX records.
if my_mx.lookup.is_err() {
return CheckEmailOutput {
input: to_email.to_string(),
+ is_reachable: Reachable::Invalid,
mx: Ok(my_mx),
syntax: my_syntax,
..Default::default()
@@ -135,9 +156,10 @@ async fn check_single_email(input: CheckEmailInput) -> CheckEmailOutput {
}
let my_misc = check_misc(&my_syntax);
- debug!(
+ log::debug!(
target: LOG_TARGET,
- "Found the following misc details: {:?}", my_misc
+ "Found the following misc details: {:?}",
+ my_misc
);
// Create one future per lookup result.
@@ -177,6 +199,7 @@ async fn check_single_email(input: CheckEmailInput) -> CheckEmailOutput {
CheckEmailOutput {
input: to_email.to_string(),
+ is_reachable: calculate_reachable(&my_misc, &my_smtp),
misc: Ok(my_misc),
mx: Ok(my_mx),
smtp: my_smtp,
diff --git a/core/src/smtp.rs b/core/src/smtp.rs
index 30e5aec6..89f36b57 100644
--- a/core/src/smtp.rs
+++ b/core/src/smtp.rs
@@ -88,7 +88,7 @@ impl From<SocksError> for SmtpError {
macro_rules! try_smtp (
($res: expr, $client: ident, $host: expr, $port: expr) => ({
if let Err(err) = $res {
- debug!(target: LOG_TARGET, "Closing {}:{}, because of error '{}'.", $host, $port, err);
+ log::debug!(target: LOG_TARGET, "Closing {}:{}, because of error '{}'.", $host, $port, err);
$client.close().await?;
return Err(err.into());
@@ -112,7 +112,7 @@ async fn connect_to_host(
.into_transport();
// Connect to the host. If the proxy argument is set, use it.
- debug!(target: LOG_TARGET, "Connecting to {}:{}", host, port);
+ log::debug!(target: LOG_TARGET, "Connecting to {}:{}", host, port);
if let Some(proxy) = proxy {
let stream = Socks5Stream::connect(
(proxy.host.as_ref(), proxy.port),
diff --git a/core/src/util/input_output.rs b/core/src/util/input_output.rs
index 977fff46..02449a06 100644
--- a/core/src/util/input_output.rs
+++ b/core/src/util/input_output.rs
@@ -82,11 +82,33 @@ impl CheckEmailInput {
}
}
+/// An enum to describe how confident we are that the recipient address is
+/// real.
+#[derive(Debug, Serialize)]
+pub enum Reachable {
+ /// The email is safe to send.
+ Safe,
+ /// The email address appears to exist, but has quality issues that may
+ /// result in low engagement or a bounce. Emails are classified as risky
+ /// when one of the following happens:
+ /// - catch-all email,
+ /// - disposable email,
+ /// - role-based address,
+ /// - full inbox.
+ Risky,
+ /// Emails that don't exist or are syntactically incorrect. Do not send to
+ /// these emails.
+ Invalid,
+ /// We're unable to get a valid response from the recipient's email server.
+ Unknown,
+}
+
/// The result of the [check_email](check_email) function.
#[derive(Debug)]
pub struct CheckEmailOutput {
/// Input by the user.
pub input: String,
+ pub is_reachable: Reachable,
/// Misc details about the email address.
pub misc: Result<MiscDetails, MiscError>,
/// Details about the MX host.
@@ -101,6 +123,7 @@ impl Default for CheckEmailOutput {
fn default() -> Self {
CheckEmailOutput {
input: String::default(),
+ is_reachable: Reachable::Unknown,
misc: Ok(MiscDetails::default()),
mx: Ok(MxDetails::default()),
smtp: Ok(SmtpDetails::default()),
@@ -123,6 +146,7 @@ impl Serialize for CheckEmailOutput {
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("input", &self.input)?;
+ map.serialize_entry("is_reachable", &self.is_reachable)?;
match &self.misc {
Ok(t) => map.serialize_entry("misc", &t)?,
Err(error) => map.serialize_entry("misc", &MyError { error })?,
diff --git a/test_suite/Cargo.toml b/test_suite/Cargo.toml
index 4ea339c2..05fc793f 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.8.1"
+version = "0.8.2"
edition = "2018"
publish = false
diff --git a/test_suite/src/fixtures/foo.json b/test_suite/src/fixtures/foo.json
index 55236e0e..d58c5871 100644
--- a/test_suite/src/fixtures/foo.json
+++ b/test_suite/src/fixtures/foo.json
@@ -1,5 +1,6 @@
{
"input": "foo",
+ "is_reachable": "invalid",
"misc": { "is_disposable": false, "is_role_account": false },
"mx": { "accepts_mail": false, "records": [] },
"smtp": {
diff --git a/test_suite/src/fixtures/[email protected] b/test_suite/src/fixtures/[email protected]
index e0ad0653..f2252afe 100644
--- a/test_suite/src/fixtures/[email protected]
+++ b/test_suite/src/fixtures/[email protected]
@@ -1,5 +1,6 @@
{
"input": "[email protected]",
+ "is_reachable": "invalid",
"misc": { "is_disposable": false, "is_role_account": false },
"mx": { "accepts_mail": false, "records": [] },
"smtp": {
diff --git a/test_suite/src/fixtures/[email protected] b/test_suite/src/fixtures/[email protected]
index 4dbc1174..9d3b4c06 100644
--- a/test_suite/src/fixtures/[email protected]
+++ b/test_suite/src/fixtures/[email protected]
@@ -1,5 +1,6 @@
{
"input": "foo@bar",
+ "is_reachable": "invalid",
"misc": { "is_disposable": false, "is_role_account": false },
"mx": { "accepts_mail": false, "records": [] },
"smtp": {
diff --git a/test_suite/src/fixtures/[email protected] b/test_suite/src/fixtures/[email protected]
index 00035fbd..4236b82b 100644
--- a/test_suite/src/fixtures/[email protected]
+++ b/test_suite/src/fixtures/[email protected]
@@ -1,5 +1,6 @@
{
"input": "[email protected]",
+ "is_reachable": "invalid",
"misc": { "is_disposable": false, "is_role_account": false },
"mx": {
"accepts_mail": true,