Resultify Smtp::send

This commit is contained in:
Alexander Krotov
2019-11-29 16:08:37 +01:00
committed by Floris Bruynooghe
parent 20ce5f6967
commit 14287b12ae
2 changed files with 34 additions and 13 deletions

View File

@@ -1,6 +1,8 @@
use lettre::smtp::client::net::*;
use lettre::*;
use failure::Fail;
use crate::constants::*;
use crate::context::Context;
use crate::error::Error;
@@ -17,6 +19,16 @@ pub struct Smtp {
from: Option<EmailAddress>,
}
#[derive(Debug, Fail)]
pub enum SmtpError {
#[fail(display = "Envelope error: {}", _0)]
EnvelopeError(lettre::error::Error),
#[fail(display = "Send error: {}", _0)]
SendError(lettre::smtp::error::Error),
#[fail(display = "SMTP has no transport")]
NoTransport,
}
impl Smtp {
/// Create a new Smtp instances.
pub fn new() -> Self {
@@ -144,7 +156,7 @@ impl Smtp {
recipients: Vec<EmailAddress>,
message: Vec<u8>,
job_id: u32,
) -> Result<(), Error> {
) -> Result<(), SmtpError> {
let message_len = message.len();
let recipients_display = recipients
@@ -155,10 +167,8 @@ impl Smtp {
if let Some(ref mut transport) = self.transport {
let envelope = match Envelope::new(self.from.clone(), recipients) {
Ok(env) => env,
Err(err) => {
bail!("{}", err);
}
Ok(envelope) => envelope,
Err(e) => return Err(SmtpError::EnvelopeError(e)),
};
let mail = SendableEmail::new(
envelope,
@@ -175,15 +185,14 @@ impl Smtp {
self.transport_connected = true;
Ok(())
}
Err(err) => {
bail!("SMTP failed len={}: error: {}", message_len, err);
}
Err(err) => return Err(SmtpError::SendError(err)),
}
} else {
bail!(
"uh? SMTP has no transport, failed to send to {:?}",
recipients_display
warn!(
context,
"uh? SMTP has no transport, failed to send to {}", recipients_display
);
return Err(SmtpError::NoTransport);
}
}
}