Skip to content

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

feat: Add optional timeout on smtp verification (#611)

Amaury Martinyc70de7dcac18
diff --git a/Cargo.lock b/Cargo.lock
index f7c58cb7..bc4c9ead 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -332,9 +332,10 @@ checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "check-if-email-exists"
-version = "0.8.13"
+version = "0.8.14"
dependencies = [
"async-smtp",
+ "async-std",
"async-std-resolver",
"fast-socks5",
"futures",
@@ -345,12 +346,13 @@ dependencies = [
"reqwest",
"serde",
"serde_json",
+ "tokio",
"trust-dns-proto",
]
[[package]]
name = "check-if-email-exists-cli"
-version = "0.8.13"
+version = "0.8.14"
dependencies = [
"check-if-email-exists",
"clap",
@@ -363,7 +365,7 @@ dependencies = [
[[package]]
name = "check-if-email-exists-test-suite"
-version = "0.8.13"
+version = "0.8.14"
dependencies = [
"check-if-email-exists",
"serde",
diff --git a/Cargo.toml b/Cargo.toml
index 8198073f..9b8f6f97 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "check-if-email-exists-cli"
-version = "0.8.13"
+version = "0.8.14"
default-run = "check_if_email_exists"
edition = "2018"
license = "AGPL-3.0"
diff --git a/Dockerfile b/Dockerfile
index 946f2ef8..d5936065 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.13
+ENV CIEE_VERSION 0.8.14
# Install needed libraries
RUN apk update && \
diff --git a/README.md b/README.md
index ff6c5cc0..eee8e1f6 100644
--- a/README.md
+++ b/README.md
@@ -95,7 +95,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.13
+check_if_email_exists 0.8.14
Check if an email address exists without sending any email.
USAGE:
diff --git a/core/Cargo.toml b/core/Cargo.toml
index 1ddedfb6..5bacc413 100644
--- a/core/Cargo.toml
+++ b/core/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "check-if-email-exists"
-version = "0.8.13"
+version = "0.8.14"
authors = ["Amaury Martiny <[email protected]>"]
categories = ["email"]
description = "Check if an email address exists without sending any email"
@@ -18,6 +18,7 @@ travis-ci = { repository = "amaurymartiny/check-if-email-exists", service = "git
[dependencies]
async-smtp = { version = "0.3", features = ["socks5"] }
+async-std = "1.6"
async-std-resolver = "0.19"
fast-socks5 = "0.3"
futures = "0.3"
@@ -29,3 +30,6 @@ reqwest = { version = "0.10", features = ["json", "socks"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
trust-dns-proto = "0.19"
+
+[dev-dependencies]
+tokio = { version = "0.2", features = ["blocking"] }
diff --git a/core/src/lib.rs b/core/src/lib.rs
index 002f9ba7..ac8cbed7 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -206,6 +206,7 @@ pub async fn check_email(inputs: &CheckEmailInput) -> Vec<CheckEmailOutput> {
from_email: inputs.from_email.clone(),
hello_name: inputs.hello_name.clone(),
proxy: inputs.proxy.clone(),
+ smtp_timeout: inputs.smtp_timeout,
yahoo_use_api: inputs.yahoo_use_api,
}
});
diff --git a/core/src/smtp/mod.rs b/core/src/smtp/mod.rs
index 1281ed3d..b3802606 100644
--- a/core/src/smtp/mod.rs
+++ b/core/src/smtp/mod.rs
@@ -25,6 +25,7 @@ use async_smtp::{
},
ClientSecurity, EmailAddress, SmtpClient, SmtpTransport,
};
+use async_std::future;
use fast_socks5::{
client::{Config, Socks5Stream},
Result, SocksError,
@@ -73,6 +74,9 @@ pub enum SmtpError {
/// Error when communicating with SMTP server.
#[serde(serialize_with = "ser_with_display")]
SmtpError(AsyncSmtpError),
+ /// Time-out error.
+ #[serde(serialize_with = "ser_with_display")]
+ TimeoutError(future::TimeoutError),
/// Error when verifying a Yahoo email.
YahooError(YahooError),
}
@@ -89,6 +93,12 @@ impl From<SocksError> for SmtpError {
}
}
+impl From<future::TimeoutError> for SmtpError {
+ fn from(error: future::TimeoutError) -> Self {
+ SmtpError::TimeoutError(error)
+ }
+}
+
impl From<YahooError> for SmtpError {
fn from(error: YahooError) -> Self {
SmtpError::YahooError(error)
@@ -333,21 +343,13 @@ async fn smtp_is_catch_all(
.map(|deliverability| deliverability.is_deliverable)
}
-/// Get all email details we can from one single `EmailAddress`.
-pub async fn check_smtp(
+async fn create_smtp_future(
to_email: &EmailAddress,
host: &Name,
port: u16,
domain: &str,
input: &CheckEmailInput,
-) -> Result<SmtpDetails, SmtpError> {
- // FIXME Is this `contains` too lenient?
- if input.yahoo_use_api && domain.to_lowercase().contains("yahoo") {
- return yahoo::check_yahoo(to_email, input)
- .await
- .map_err(|err| err.into());
- }
-
+) -> Result<(bool, Deliverability), SmtpError> {
// FIXME If the SMTP is not connectable, we should actually return an
// Ok(SmtpDetails { can_connect_smtp: false, ... }).
let mut smtp_client = connect_to_host(host, port, input).await?;
@@ -359,6 +361,31 @@ pub async fn check_smtp(
smtp_client.close().await?;
+ Ok((is_catch_all, deliverability))
+}
+
+/// Get all email details we can from one single `EmailAddress`.
+pub async fn check_smtp(
+ to_email: &EmailAddress,
+ host: &Name,
+ port: u16,
+ domain: &str,
+ input: &CheckEmailInput,
+) -> Result<SmtpDetails, SmtpError> {
+ // FIXME Is this `contains` too lenient?
+ if input.yahoo_use_api && domain.to_lowercase().contains("yahoo") {
+ return yahoo::check_yahoo(to_email, input)
+ .await
+ .map_err(|err| err.into());
+ }
+
+ let fut = create_smtp_future(to_email, host, port, domain, input);
+ let (is_catch_all, deliverability) = if let Some(smtp_timeout) = input.smtp_timeout {
+ future::timeout(smtp_timeout, fut).await??
+ } else {
+ fut.await?
+ };
+
Ok(SmtpDetails {
can_connect_smtp: true,
has_full_inbox: deliverability.has_full_inbox,
@@ -367,3 +394,28 @@ pub async fn check_smtp(
is_disabled: deliverability.is_disabled,
})
}
+
+#[cfg(test)]
+mod tests {
+ use super::{check_smtp, CheckEmailInput, SmtpError};
+ use async_smtp::EmailAddress;
+ use std::{str::FromStr, time::Duration};
+ use tokio::runtime::Runtime;
+ use trust_dns_proto::rr::Name;
+
+ #[test]
+ fn should_timeout() {
+ let mut runtime = Runtime::new().unwrap();
+
+ let to_email = EmailAddress::from_str("[email protected]").unwrap();
+ let host = Name::from_str("gmail.com").unwrap();
+ let mut input = CheckEmailInput::default();
+ input.smtp_timeout(Duration::from_millis(1));
+
+ let res = runtime.block_on(check_smtp(&to_email, &host, 25, "gmail.com", &input));
+ match res {
+ Err(SmtpError::TimeoutError(_)) => (),
+ _ => panic!("check_smtp did not time out"),
+ }
+ }
+}
diff --git a/core/src/util/input_output.rs b/core/src/util/input_output.rs
index 1fa280cc..2186c0d3 100644
--- a/core/src/util/input_output.rs
+++ b/core/src/util/input_output.rs
@@ -19,6 +19,7 @@ use crate::mx::{MxDetails, MxError};
use crate::smtp::{SmtpDetails, SmtpError};
use crate::syntax::SyntaxDetails;
use serde::{ser::SerializeMap, Serialize, Serializer};
+use std::time::Duration;
/// Perform the email verification via a specified proxy. The usage of a proxy
/// is optional.
@@ -47,6 +48,8 @@ pub struct CheckEmailInput {
/// Perform the email verification via a specified proxy. The usage of a
/// proxy is optional.
pub proxy: Option<CheckEmailInputProxy>,
+ /// Add optional timeout for the SMTP verification step.
+ pub smtp_timeout: Option<Duration>,
/// For Yahoo email addresses, use Yahoo's API instead of connecting
/// directly to their SMTP servers.
///
@@ -61,6 +64,7 @@ impl Default for CheckEmailInput {
from_email: "[email protected]".into(),
hello_name: "localhost".into(),
proxy: None,
+ smtp_timeout: None,
yahoo_use_api: true,
}
}
@@ -96,6 +100,12 @@ impl CheckEmailInput {
self
}
+ /// Add optional timeout for the SMTP verification step.
+ pub fn smtp_timeout(&mut self, duration: Duration) -> &mut CheckEmailInput {
+ self.smtp_timeout = Some(duration);
+ self
+ }
+
/// Set whether to use Yahoo's API or connecting directly to their SMTP
/// servers.
pub fn yahoo_use_api(&mut self, use_api: bool) -> &mut CheckEmailInput {
diff --git a/test_suite/Cargo.toml b/test_suite/Cargo.toml
index 9f82471f..68b04ae1 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.13"
+version = "0.8.14"
edition = "2018"
publish = false