Skip to content

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

feat: Break SmtpError into `{Helo,Connect,ConnectWithStream,MailFrom,RcptTo,Close}Error` (#1055)

Amaury64e5193c48a6
diff --git a/Cargo.lock b/Cargo.lock
index 264b03d8..7f23a6f5 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -318,7 +318,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "check-if-email-exists"
-version = "0.8.26"
+version = "0.8.27"
dependencies = [
"async-native-tls",
"async-recursion",
@@ -340,7 +340,7 @@ dependencies = [
[[package]]
name = "check-if-email-exists-cli"
-version = "0.8.26"
+version = "0.8.27"
dependencies = [
"check-if-email-exists",
"clap",
diff --git a/Cargo.toml b/Cargo.toml
index 42c943a6..3f182728 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "check-if-email-exists-cli"
-version = "0.8.26"
+version = "0.8.27"
default-run = "check_if_email_exists"
edition = "2018"
description = "Check if an email address exists without sending any email."
diff --git a/Dockerfile b/Dockerfile
index 53c6614f..1c4c561f 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -8,7 +8,7 @@ FROM alpine
# `ciee` stands for check-if-email-exists
WORKDIR /ciee
# Fetch latest version
-ENV CIEE_VERSION 0.8.24
+ENV CIEE_VERSION 0.8.27
# Install needed libraries
RUN apk update && \
diff --git a/README.md b/README.md
index b30cf62c..3c75399f 100644
--- a/README.md
+++ b/README.md
@@ -88,7 +88,7 @@ Head to the [releases page](https://github.com/reacherhq/check-if-email-exists/r
```
> $ check_if_email_exists --help
-check_if_email_exists 0.8.26
+check_if_email_exists 0.8.27
Check if an email address exists without sending any email.
USAGE:
diff --git a/core/Cargo.toml b/core/Cargo.toml
index 35982b77..8e90a22a 100644
--- a/core/Cargo.toml
+++ b/core/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "check-if-email-exists"
-version = "0.8.26"
+version = "0.8.27"
authors = ["Amaury <[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 aaa801a6..3af8e5cd 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -142,7 +142,7 @@ async fn check_single_email(input: CheckEmailInput) -> CheckEmailOutput {
};
log::debug!(
target: LOG_TARGET,
- "email={} Found the following MX hosts {:?}",
+ "email={} Found the following MX hosts: {:?}",
to_email,
my_mx
);
@@ -204,10 +204,17 @@ async fn check_single_email(input: CheckEmailInput) -> CheckEmailOutput {
/// list of results.
///
/// Please note that checking multiple emails at once (by putting multiple
-/// emails in the `inputs.to_emails` Vec) is still a beta feature, and not
+/// emails in the `inputs.to_emails` Vec) is still a **beta** feature, and not
/// fully optimized. For more info, see #65
/// <https://github.com/reacherhq/check-if-email-exists/issues/65>.
pub async fn check_email(inputs: &CheckEmailInput) -> Vec<CheckEmailOutput> {
+ if inputs.to_emails.len() > 1 {
+ log::warn!(
+ target: LOG_TARGET,
+ "Verifying multiple emails with this library is still a beta feature, please don't overuse it. See reacherhq/check-if-email-exists #65."
+ );
+ }
+
// FIXME Obviously, the below `join_all` is not optimal. Some optimizations
// include:
// - if multiple email addresses share the same domain, we should only do
diff --git a/core/src/smtp/mod.rs b/core/src/smtp/mod.rs
index af8896d5..9f784966 100644
--- a/core/src/smtp/mod.rs
+++ b/core/src/smtp/mod.rs
@@ -64,8 +64,30 @@ pub enum SmtpError {
/// Error if we're using a SOCKS5 proxy.
#[serde(serialize_with = "ser_with_display")]
SocksError(SocksError),
+ /// Error when communicating with SMTP server, HELO phase.
+ #[serde(serialize_with = "ser_with_display")]
+ HeloError(AsyncSmtpError),
+ /// Error when communicating with SMTP server, connect phase.
+ #[serde(serialize_with = "ser_with_display")]
+ ConnectError(AsyncSmtpError),
+ /// Error when communicating with SMTP server, connect_with_stream phase.
+ #[serde(serialize_with = "ser_with_display")]
+ ConnectWithStreamError(AsyncSmtpError),
+ /// Error when communicating with SMTP server, MAIL FROM phase.
+ #[serde(serialize_with = "ser_with_display")]
+ MailFromError(AsyncSmtpError),
+ /// Error when communicating with SMTP server, RCPT TO phase.
+ #[serde(serialize_with = "ser_with_display")]
+ RcptToError(AsyncSmtpError),
+ /// Error when communicating with SMTP server, close phase.
+ #[serde(serialize_with = "ser_with_display")]
+ CloseError(AsyncSmtpError),
/// Error when communicating with SMTP server.
#[serde(serialize_with = "ser_with_display")]
+ #[deprecated(
+ since = "0.8.27",
+ note = "SMTP errors have been broken up into 6 phases, see all `Smtp*Error`s"
+ )]
SmtpError(AsyncSmtpError),
/// Time-out error.
#[serde(serialize_with = "ser_with_display")]
@@ -74,12 +96,6 @@ pub enum SmtpError {
YahooError(YahooError),
}
-impl From<AsyncSmtpError> for SmtpError {
- fn from(error: AsyncSmtpError) -> Self {
- SmtpError::SmtpError(error)
- }
-}
-
impl From<SocksError> for SmtpError {
fn from(error: SocksError) -> Self {
SmtpError::SocksError(error)
@@ -100,12 +116,13 @@ impl From<YahooError> for SmtpError {
/// Try to send an smtp command, close and return Err if fails.
macro_rules! try_smtp (
- ($res: expr, $client: ident, $to_email: expr, $host: expr, $port: expr) => ({
+ ($res: expr, $client: ident, $to_email: expr, $host: expr, $port: expr, $err_type: expr) => ({
if let Err(err) = $res {
log::debug!(target: LOG_TARGET, "email={} Closing {}:{}, because of error '{:?}'.", $to_email, $host, $port, err);
- $client.close().await?;
+ // Try to close the connection, but ignore if there's an error.
+ let _ = $client.close().await;
- return Err(err.into());
+ return Err($err_type(err));
}
})
);
@@ -125,7 +142,8 @@ async fn connect_to_host(
input.smtp_security.to_client_security(tls_params)
};
let mut smtp_client = SmtpClient::with_security((host.to_utf8().as_ref(), port), security)
- .await?
+ .await
+ .map_err(SmtpError::HeloError)?
// FIXME Do not clone?
.hello_name(ClientId::Domain(input.hello_name.clone()))
.timeout(Some(Duration::new(30, 0))) // Set timeout to 30s
@@ -155,7 +173,8 @@ async fn connect_to_host(
smtp_client,
input.to_emails[0],
host,
- port
+ port,
+ SmtpError::ConnectWithStreamError
);
} else {
try_smtp!(
@@ -163,7 +182,8 @@ async fn connect_to_host(
smtp_client,
input.to_emails[0],
host,
- port
+ port,
+ SmtpError::ConnectError
);
}
@@ -183,7 +203,8 @@ async fn connect_to_host(
smtp_client,
input.to_emails[0],
host,
- port
+ port,
+ SmtpError::MailFromError
);
Ok(smtp_client)
@@ -334,7 +355,7 @@ async fn email_deliverable(
});
}
- Err(SmtpError::SmtpError(err))
+ Err(SmtpError::RcptToError(err))
}
}
}
@@ -391,6 +412,11 @@ async fn create_smtp_future(
// so we can only check for "io: incomplete" SMTP error being returned.
// https://github.com/async-email/async-smtp/issues/37
if is_io_incomplete_smtp_error(&result) {
+ log::debug!(
+ target: LOG_TARGET,
+ "Got `io: incomplete` error, reconnecting."
+ );
+
let _ = smtp_client.close().await;
smtp_client = connect_to_host(host, port, input).await?;
result = email_deliverable(&mut smtp_client, to_email).await;
@@ -399,18 +425,22 @@ async fn create_smtp_future(
result?
};
- smtp_client.close().await?;
+ smtp_client.close().await.map_err(SmtpError::CloseError)?;
Ok((is_catch_all, deliverability))
}
/// Indicates whether the given [`Result`] represents an `io: incomplete`
-/// [`SmtpError::SmtpError`].
+/// [`SmtpError::Error`].
fn is_io_incomplete_smtp_error<T>(result: &Result<T, SmtpError>) -> bool {
- if let Err(SmtpError::SmtpError(AsyncSmtpError::Io(err))) = result {
- err.to_string().as_str() == "incomplete"
- } else {
- false
+ match result {
+ Err(SmtpError::HeloError(AsyncSmtpError::Io(err)))
+ | Err(SmtpError::ConnectError(AsyncSmtpError::Io(err)))
+ | Err(SmtpError::ConnectWithStreamError(AsyncSmtpError::Io(err)))
+ | Err(SmtpError::MailFromError(AsyncSmtpError::Io(err)))
+ | Err(SmtpError::RcptToError(AsyncSmtpError::Io(err)))
+ | Err(SmtpError::CloseError(AsyncSmtpError::Io(err))) => err.to_string().as_str() == "incomplete",
+ _ => false,
}
}