use std::borrow::Cow;
use lettre::{
AsyncSmtpTransport, AsyncTransport, Message, message::Mailbox,
transport::smtp::response::Severity,
};
use crate::{EmailEnvelopeDetails, MailListError, MailListResult};
#[derive(Debug)]
pub struct Smtps<'a> {
pub(crate) from: Mailbox,
pub(crate) reply_to: Option<Mailbox>,
pub(crate) hello_name: Option<Cow<'a, str>>,
#[cfg(feature = "tokio-runtime")]
pub(crate) mailer: AsyncSmtpTransport<lettre::Tokio1Executor>,
}
impl<'a> Smtps<'a> {
pub fn from(&self) -> &Mailbox {
&self.from
}
pub fn reply_to(&self) -> Option<&Mailbox> {
self.reply_to.as_ref()
}
pub fn hello_name(&self) -> Option<&str> {
self.hello_name.as_ref().map(|value| value.as_ref())
}
#[cfg(feature = "tokio-runtime")]
pub fn mailer(&self) -> &AsyncSmtpTransport<lettre::Tokio1Executor> {
&self.mailer
}
pub async fn send(&self, message: &EmailEnvelopeDetails) -> MailListResult<()> {
let email = Message::builder().from(self.from().clone());
let email = if let Some(reply_to) = self.reply_to() {
email.reply_to(reply_to.clone())
} else {
email
};
let email = email
.to(message
.to
.parse::<Mailbox>()
.map_err(|error| MailListError::Mailer(error.to_string()))?)
.subject(message.subject.as_str())
.header(lettre::message::header::ContentType::TEXT_HTML)
.body(message.body.to_string())
.map_err(|error| MailListError::Mailer(error.to_string()))?;
let response = self
.mailer
.send(email)
.await
.map_err(|error| MailListError::MailDelivery(error.to_string()))?;
if response.code().severity == Severity::PositiveCompletion {
Ok(())
} else {
Err(MailListError::Smtps(
response.message().map(|msg| msg.to_string()).collect(),
))
}
}
pub async fn test_connection(&self) -> MailListResult<bool> {
self.mailer
.test_connection()
.await
.map_err(|error| MailListError::Mailer(error.to_string()))
}
}