Skip to content

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

Start with insecure connection

Amaury Martinyf622478ffdd0
diff --git a/src/main.rs b/src/main.rs
index c15f2bc6..1e4e5817 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -9,7 +9,7 @@ extern crate rayon;
extern crate trust_dns_resolver;
use clap::App;
-use lettre::smtp::{SMTP_PORT, SUBMISSION_PORT};
+use lettre::smtp::{SMTP_PORT, SUBMISSIONS_PORT, SUBMISSION_PORT};
use rayon::prelude::*;
use std::process;
@@ -23,7 +23,7 @@ fn main() {
let yaml = load_yaml!("cli.yml");
let matches = App::from_yaml(yaml).get_matches();
- let from_email = matches.value_of("from").unwrap_or("[email protected]");
+ let from_email = matches.value_of("from").unwrap_or("[email protected]");
// Calling .unwrap() is safe here because "TO" is required
let to_email = matches.value_of("TO").unwrap();
@@ -41,7 +41,7 @@ fn main() {
debug!("Getting MX lookup...");
let hosts = mx_hosts::get_mx_lookup(domain);
info!("Found the following MX hosts {:?}", hosts);
- let ports = vec![SMTP_PORT, SUBMISSION_PORT, 465];
+ let ports = vec![SMTP_PORT, SUBMISSION_PORT, SUBMISSIONS_PORT];
let mut combinations = Vec::new(); // `(host, port)` combination
for port in ports.into_iter() {
for host in hosts.iter() {
diff --git a/src/smtp.rs b/src/smtp.rs
index 990c2b04..110588ad 100644
--- a/src/smtp.rs
+++ b/src/smtp.rs
@@ -13,7 +13,7 @@ macro_rules! try_smtp (
Ok(res) => {
if !res.is_positive() {
if let Some(message) = res.first_line() {
- debug!("{}", message);
+ debug!("Error: {}", message);
}
$client.close();
return false;
@@ -21,7 +21,7 @@ macro_rules! try_smtp (
res
},
Err(err) => {
- debug!("{}", err);
+ debug!("Error: {}", err);
$client.close();
return false;
},
@@ -38,12 +38,10 @@ pub fn email_exists(from: &str, to: &str, host: &Name, port: u16) -> bool {
return false;
}
- let tls_builder = TlsConnector::builder();
- let tls_parameters = ClientTlsParameters::new(host.to_string(), tls_builder.build().unwrap());
-
- // Connect to the host
+ // Connect to the host. Start with insecure connection and use `STARTTLS`
+ // when available
try_smtp!(
- email_client.connect(&(host.to_utf8().as_str(), port), Some(&tls_parameters)),
+ email_client.connect(&(host.to_utf8().as_str(), port), None),
email_client
);
@@ -55,7 +53,7 @@ pub fn email_exists(from: &str, to: &str, host: &Name, port: u16) -> bool {
let server_info = ServerInfo::from_response(&ehlo_response);
debug!("Server info: {}", server_info.as_ref().unwrap());
- // Send starttls if available
+ // Send `STARTTLS` if available
if server_info
.as_ref()
.unwrap()
@@ -63,6 +61,15 @@ pub fn email_exists(from: &str, to: &str, host: &Name, port: u16) -> bool {
{
try_smtp!(email_client.command(StarttlsCommand), email_client);
+ let tls_builder = TlsConnector::builder();
+ let tls_parameters =
+ ClientTlsParameters::new(host.to_string(), tls_builder.build().unwrap());
+ if let Err(err) = email_client.upgrade_tls_stream(&tls_parameters) {
+ debug!("{}", err);
+ email_client.close();
+ return false;
+ }
+
debug!("Connection is encrypted.");
}