remove redundant state from smtp

This commit is contained in:
holger krekel
2019-12-08 01:13:34 +01:00
parent a8e9a1fbe5
commit 93e8cca02f
3 changed files with 12 additions and 19 deletions

View File

@@ -203,8 +203,8 @@ impl Job {
match task::block_on(smtp.send(context, recipients_list, body, self.job_id)) { match task::block_on(smtp.send(context, recipients_list, body, self.job_id)) {
Err(crate::smtp::send::Error::SendError(err)) => { Err(crate::smtp::send::Error::SendError(err)) => {
// Remote error, retry later. // Remote error, retry later.
smtp.disconnect();
info!(context, "SMTP failed to send: {}", err); info!(context, "SMTP failed to send: {}", err);
smtp.disconnect();
self.try_again_later(TryAgain::AtOnce, Some(err.to_string())); self.try_again_later(TryAgain::AtOnce, Some(err.to_string()));
} }
Err(crate::smtp::send::Error::EnvelopeError(err)) => { Err(crate::smtp::send::Error::EnvelopeError(err)) => {

View File

@@ -37,7 +37,6 @@ pub type Result<T> = std::result::Result<T, Error>;
pub struct Smtp { pub struct Smtp {
#[debug_stub(some = "SmtpTransport")] #[debug_stub(some = "SmtpTransport")]
transport: Option<async_smtp::smtp::SmtpTransport>, transport: Option<async_smtp::smtp::SmtpTransport>,
transport_connected: bool,
/// Email address we are sending from. /// Email address we are sending from.
from: Option<EmailAddress>, from: Option<EmailAddress>,
} }
@@ -50,16 +49,12 @@ impl Smtp {
/// Disconnect the SMTP transport and drop it entirely. /// Disconnect the SMTP transport and drop it entirely.
pub fn disconnect(&mut self) { pub fn disconnect(&mut self) {
if self.transport.is_none() || !self.transport_connected { if let Some(ref mut transport) = self.transport.take() {
return; transport.close();
} }
let mut transport = self.transport.take().unwrap();
transport.close();
self.transport_connected = false;
} }
/// Check if a connection already exists. /// check whether we are connected
pub fn is_connected(&self) -> bool { pub fn is_connected(&self) -> bool {
self.transport.is_some() self.transport.is_some()
} }
@@ -143,7 +138,6 @@ impl Smtp {
task::block_on(trans.connect()).map_err(Error::ConnectionFailure)?; task::block_on(trans.connect()).map_err(Error::ConnectionFailure)?;
self.transport = Some(trans); self.transport = Some(trans);
self.transport_connected = true;
context.call_cb(Event::SmtpConnected(format!( context.call_cb(Event::SmtpConnected(format!(
"SMTP-LOGIN as {} ok", "SMTP-LOGIN as {} ok",
lp.send_user, lp.send_user,

View File

@@ -36,22 +36,21 @@ impl Smtp {
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join(","); .join(",");
let envelope =
Envelope::new(self.from.clone(), recipients).map_err(Error::EnvelopeError)?;
let mail = SendableEmail::new(
envelope,
format!("{}", job_id), // only used for internal logging
message,
);
if let Some(ref mut transport) = self.transport { if let Some(ref mut transport) = self.transport {
let envelope =
Envelope::new(self.from.clone(), recipients).map_err(Error::EnvelopeError)?;
let mail = SendableEmail::new(
envelope,
format!("{}", job_id), // only used for internal logging
message,
);
transport.send(mail).await.map_err(Error::SendError)?; transport.send(mail).await.map_err(Error::SendError)?;
context.call_cb(Event::SmtpMessageSent(format!( context.call_cb(Event::SmtpMessageSent(format!(
"Message len={} was smtp-sent to {}", "Message len={} was smtp-sent to {}",
message_len, recipients_display message_len, recipients_display
))); )));
self.transport_connected = true;
Ok(()) Ok(())
} else { } else {
warn!( warn!(