Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reconnect auto-closed SMTP connections by foreign server #825

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion core/src/smtp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,14 +366,38 @@ async fn create_smtp_future(
is_disabled: false,
}
} else {
email_deliverable(&mut smtp_client, to_email).await?
let mut result = email_deliverable(&mut smtp_client, to_email).await;

// Some SMTP servers automatically close the connection after an error,
// so we should reconnect to perform a next command.
//
// Unfortunately `smtp_client.is_connected()` doesn't report about this,
// 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) {
tyranron marked this conversation as resolved.
Show resolved Hide resolved
let _ = smtp_client.close().await;
smtp_client = connect_to_host(host, port, input).await?;
result = email_deliverable(&mut smtp_client, to_email).await;
}

result?
};

smtp_client.close().await?;

Ok((is_catch_all, deliverability))
}

/// Indicates whether the given [`Result`] represents an `io: incomplete`
/// [`SmtpError::SmtpError`].
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
}
}

/// Get all email details we can from one single `EmailAddress`.
pub async fn check_smtp(
to_email: &EmailAddress,
Expand Down