Skip to content

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

feat: Make using Yahoo API optional (#460)

Amaury Martiny1e42f0abef27
diff --git a/core/src/lib.rs b/core/src/lib.rs
index fa2a68c6..84ac26fb 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -61,12 +61,11 @@ pub mod smtp;
pub mod syntax;
mod util;
-use async_smtp::{smtp::SMTP_PORT, EmailAddress};
+use async_smtp::smtp::SMTP_PORT;
use futures::future;
use misc::{check_misc, MiscDetails};
use mx::check_mx;
use smtp::{check_smtp, SmtpDetails, SmtpError};
-use std::str::FromStr;
use syntax::check_syntax;
use util::constants::LOG_TARGET;
@@ -98,14 +97,6 @@ fn calculate_reachable(misc: &MiscDetails, smtp: &Result<SmtpDetails, SmtpError>
///
/// 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(|_| {
- log::warn!(
- "Inputted from_email \"{}\" is not a valid email, using \"[email protected]\" instead",
- input.from_email
- );
- EmailAddress::from_str("[email protected]").expect("This is a valid email. qed.")
- });
-
let to_email = &input.to_emails[0];
log::debug!(target: LOG_TARGET, "Checking email \"{}\"", to_email);
@@ -176,13 +167,11 @@ async fn check_single_email(input: CheckEmailInput) -> CheckEmailOutput {
.address
.as_ref()
.expect("We already checked that the email has valid format. qed."),
- &from_email,
host.exchange(),
// FIXME We could add ports 465 and 587 too.
SMTP_PORT,
my_syntax.domain.as_ref(),
- input.hello_name.as_ref(),
- &input.proxy,
+ &input,
);
// https://rust-lang.github.io/async-book/04_pinning/01_chapter.html
@@ -226,6 +215,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(),
+ yahoo_use_api: inputs.yahoo_use_api,
}
});
future::join_all(inputs.map(check_single_email)).await
diff --git a/core/src/smtp/mod.rs b/core/src/smtp/mod.rs
index 7ad5cc98..8a98d6db 100644
--- a/core/src/smtp/mod.rs
+++ b/core/src/smtp/mod.rs
@@ -16,7 +16,7 @@
mod yahoo;
-use super::util::{constants::LOG_TARGET, input_output::CheckEmailInputProxy};
+use super::util::{constants::LOG_TARGET, input_output::CheckEmailInput};
use crate::util::ser_with_display::ser_with_display;
use async_smtp::{
smtp::{
@@ -31,6 +31,7 @@ use fast_socks5::{
};
use rand::{distributions::Alphanumeric, Rng};
use serde::Serialize;
+use std::str::FromStr;
use std::time::Duration;
use trust_dns_proto::rr::Name;
use yahoo::YahooError;
@@ -108,22 +109,21 @@ macro_rules! try_smtp (
/// Attempt to connect to host via SMTP, and return SMTP client on success.
async fn connect_to_host(
- from_email: &EmailAddress,
host: &Name,
port: u16,
- hello_name: &str,
- proxy: &Option<CheckEmailInputProxy>,
+ input: &CheckEmailInput,
) -> Result<SmtpTransport, SmtpError> {
let mut smtp_client =
SmtpClient::with_security((host.to_utf8().as_ref(), port), ClientSecurity::None)
.await?
- .hello_name(ClientId::Domain(hello_name.into()))
+ // FIXME Do not clone?
+ .hello_name(ClientId::Domain(input.hello_name.clone()))
.timeout(Some(Duration::new(30, 0))) // Set timeout to 30s
.into_transport();
// Connect to the host. If the proxy argument is set, use it.
log::debug!(target: LOG_TARGET, "Connecting to {}:{}", host, port);
- if let Some(proxy) = proxy {
+ if let Some(proxy) = &input.proxy {
let stream = Socks5Stream::connect(
(proxy.host.as_ref(), proxy.port),
host.to_utf8(),
@@ -145,6 +145,13 @@ async fn connect_to_host(
}
// "MAIL FROM: [email protected]"
+ let from_email = EmailAddress::from_str(input.from_email.as_ref()).unwrap_or_else(|_| {
+ log::warn!(
+ "Inputted from_email \"{}\" is not a valid email, using \"[email protected]\" instead",
+ input.from_email
+ );
+ EmailAddress::from_str("[email protected]").expect("This is a valid email. qed.")
+ });
try_smtp!(
smtp_client
// FIXME Do not clone?
@@ -322,21 +329,19 @@ async fn smtp_is_catch_all(
/// Get all email details we can from one single `EmailAddress`.
pub async fn check_smtp(
to_email: &EmailAddress,
- from_email: &EmailAddress,
host: &Name,
port: u16,
domain: &str,
- hello_name: &str,
- proxy: &Option<CheckEmailInputProxy>,
+ input: &CheckEmailInput,
) -> Result<SmtpDetails, SmtpError> {
// FIXME Is this `contains` too lenient?
- if domain.to_lowercase().contains("yahoo") {
+ if input.yahoo_use_api && domain.to_lowercase().contains("yahoo") {
return yahoo::check_yahoo(to_email).await.map_err(|err| err.into());
}
// 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(from_email, host, port, hello_name, proxy).await?;
+ let mut smtp_client = connect_to_host(host, port, input).await?;
let is_catch_all = smtp_is_catch_all(&mut smtp_client, domain)
.await
diff --git a/core/src/util/input_output.rs b/core/src/util/input_output.rs
index ec66454b..1fa280cc 100644
--- a/core/src/util/input_output.rs
+++ b/core/src/util/input_output.rs
@@ -47,6 +47,23 @@ pub struct CheckEmailInput {
/// Perform the email verification via a specified proxy. The usage of a
/// proxy is optional.
pub proxy: Option<CheckEmailInputProxy>,
+ /// For Yahoo email addresses, use Yahoo's API instead of connecting
+ /// directly to their SMTP servers.
+ ///
+ /// Defaults to true.
+ pub yahoo_use_api: bool,
+}
+
+impl Default for CheckEmailInput {
+ fn default() -> Self {
+ CheckEmailInput {
+ to_emails: vec![],
+ from_email: "[email protected]".into(),
+ hello_name: "localhost".into(),
+ proxy: None,
+ yahoo_use_api: true,
+ }
+ }
}
impl CheckEmailInput {
@@ -54,9 +71,7 @@ impl CheckEmailInput {
pub fn new(to_emails: Vec<String>) -> CheckEmailInput {
CheckEmailInput {
to_emails,
- from_email: "[email protected]".into(),
- hello_name: "localhost".into(),
- proxy: None,
+ ..Default::default()
}
}
@@ -80,6 +95,13 @@ impl CheckEmailInput {
});
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 {
+ self.yahoo_use_api = use_api;
+ self
+ }
}
/// An enum to describe how confident we are that the recipient address is
diff --git a/src/cli.yml b/src/cli.yml
index b42348e0..96caa4e0 100644
--- a/src/cli.yml
+++ b/src/cli.yml
@@ -38,3 +38,7 @@ args:
- TO_EMAIL:
help: The email to check.
index: 1
+ - YAHOO_USE_API:
+ default_value: "true"
+ help: For Yahoo email addresses, use Yahoo's API instead of connecting directly to their SMTP servers.
+ long: yahoo-use-api
diff --git a/src/main.rs b/src/main.rs
index 2bf2d6df..34ddd2e1 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -53,6 +53,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
input.proxy(proxy_host.into(), proxy_port);
}
+ if let Ok(yahoo_use_api) = value_t!(matches.value_of("YAHOO_USE_API"), bool) {
+ input.yahoo_use_api(yahoo_use_api);
+ }
+
let result = check_email(&input).await;
match serde_json::to_string_pretty(&result) {