use std::fmt;
use std::sync::{Arc, OnceLock};
use std::time::Duration;
use id_effect::{Effect, Layer, fail, from_async, succeed};
use lettre::transport::smtp::authentication::Credentials;
use lettre::transport::smtp::client::{Tls, TlsParameters};
use lettre::transport::smtp::extension::ClientId;
use lettre::transport::smtp::{AsyncSmtpTransportBuilder, PoolConfig};
use lettre::{AsyncSmtpTransport, AsyncTransport, Tokio1Executor};
use crate::{MailError, Mailer, MailerEnv, Message, Sent, mailer_env};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SmtpSecurity {
Tls,
StartTls,
Plaintext,
}
impl SmtpSecurity {
pub fn default_port(self) -> u16 {
match self {
SmtpSecurity::Tls => 465,
SmtpSecurity::StartTls => 587,
SmtpSecurity::Plaintext => 25,
}
}
}
#[derive(Clone)]
pub struct SmtpConfig {
host: String,
port: Option<u16>,
security: SmtpSecurity,
username: Option<String>,
password: Option<String>,
timeout: Duration,
pool_max_size: u32,
pool_min_idle: u32,
pool_idle_timeout: Duration,
hello_name: Option<String>,
}
impl SmtpConfig {
pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);
pub const DEFAULT_POOL_MAX_SIZE: u32 = 10;
pub const DEFAULT_POOL_IDLE_TIMEOUT: Duration = Duration::from_secs(60);
fn with_security(host: impl Into<String>, security: SmtpSecurity) -> Self {
Self {
host: host.into(),
port: None,
security,
username: None,
password: None,
timeout: Self::DEFAULT_TIMEOUT,
pool_max_size: Self::DEFAULT_POOL_MAX_SIZE,
pool_min_idle: 0,
pool_idle_timeout: Self::DEFAULT_POOL_IDLE_TIMEOUT,
hello_name: None,
}
}
pub fn tls(host: impl Into<String>) -> Self {
Self::with_security(host, SmtpSecurity::Tls)
}
pub fn starttls(host: impl Into<String>) -> Self {
Self::with_security(host, SmtpSecurity::StartTls)
}
pub fn plaintext(host: impl Into<String>, port: u16) -> Self {
Self::with_security(host, SmtpSecurity::Plaintext).port(port)
}
pub fn port(mut self, port: u16) -> Self {
self.port = Some(port);
self
}
pub fn credentials(mut self, username: impl Into<String>, password: impl Into<String>) -> Self {
self.username = Some(username.into());
self.password = Some(password.into());
self
}
pub fn timeout(mut self, timeout: Duration) -> Self {
self.timeout = timeout;
self
}
pub fn pool_max_size(mut self, size: u32) -> Self {
self.pool_max_size = size.max(1);
self
}
pub fn pool_min_idle(mut self, count: u32) -> Self {
self.pool_min_idle = count;
self
}
pub fn pool_idle_timeout(mut self, timeout: Duration) -> Self {
self.pool_idle_timeout = timeout;
self
}
pub fn hello_name(mut self, name: impl Into<String>) -> Self {
self.hello_name = Some(name.into());
self
}
pub fn host(&self) -> &str {
&self.host
}
pub fn effective_port(&self) -> u16 {
self.port.unwrap_or_else(|| self.security.default_port())
}
pub fn security(&self) -> SmtpSecurity {
self.security
}
pub fn username(&self) -> Option<&str> {
self.username.as_deref()
}
fn transport_builder(&self) -> Result<AsyncSmtpTransportBuilder, MailError> {
if self.host.trim().is_empty() {
return Err(MailError::Config("the SMTP host is empty".into()));
}
let tls_for = |host: &str| {
TlsParameters::new(host.to_owned())
.map_err(|e| MailError::Config(format!("TLS for {host}: {e}")))
};
let tls = match self.security {
SmtpSecurity::Tls => Tls::Wrapper(tls_for(&self.host)?),
SmtpSecurity::StartTls => Tls::Required(tls_for(&self.host)?),
SmtpSecurity::Plaintext => Tls::None,
};
let pool = PoolConfig::new()
.max_size(self.pool_max_size)
.min_idle(self.pool_min_idle)
.idle_timeout(self.pool_idle_timeout);
let mut builder = AsyncSmtpTransport::<Tokio1Executor>::builder_dangerous(self.host.clone())
.port(self.effective_port())
.tls(tls)
.timeout(Some(self.timeout))
.pool_config(pool);
if let (Some(user), Some(password)) = (&self.username, &self.password) {
builder = builder.credentials(Credentials::new(user.clone(), password.clone()));
}
if let Some(name) = &self.hello_name {
builder = builder.hello_name(ClientId::Domain(name.clone()));
}
Ok(builder)
}
}
impl fmt::Debug for SmtpConfig {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SmtpConfig")
.field("host", &self.host)
.field("port", &self.effective_port())
.field("security", &self.security)
.field("username", &self.username)
.field("password", &self.password.as_ref().map(|_| "<redacted>"))
.field("timeout", &self.timeout)
.field("pool_max_size", &self.pool_max_size)
.field("pool_min_idle", &self.pool_min_idle)
.field("pool_idle_timeout", &self.pool_idle_timeout)
.field("hello_name", &self.hello_name)
.finish()
}
}
#[derive(Clone)]
pub struct SmtpMailer {
inner: Arc<Inner>,
}
struct Inner {
config: SmtpConfig,
builder: AsyncSmtpTransportBuilder,
transport: OnceLock<AsyncSmtpTransport<Tokio1Executor>>,
}
impl SmtpMailer {
pub fn new(config: SmtpConfig) -> Result<Self, MailError> {
let builder = config.transport_builder()?;
Ok(Self {
inner: Arc::new(Inner {
config,
builder,
transport: OnceLock::new(),
}),
})
}
pub fn layer<E>(config: SmtpConfig) -> Layer<MailerEnv, E>
where
E: From<MailError> + Send + 'static,
{
Layer::effect(move || match SmtpMailer::new(config.clone()) {
Ok(mailer) => succeed(mailer_env(mailer)),
Err(e) => fail(E::from(e)),
})
}
pub fn config(&self) -> &SmtpConfig {
&self.inner.config
}
}
impl fmt::Debug for SmtpMailer {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SmtpMailer")
.field("config", &self.inner.config)
.finish_non_exhaustive()
}
}
impl Mailer for SmtpMailer {
fn send(&self, message: Message) -> Effect<Sent, MailError> {
let inner = Arc::clone(&self.inner);
from_async(move || async move {
let id = message.new_message_id();
let mime = message.to_lettre(&id)?;
let transport = inner
.transport
.get_or_init(|| inner.builder.clone().build());
transport.send(mime).await.map_err(smtp_error)?;
Ok(Sent { id: Some(id) })
})
}
}
fn smtp_error(e: lettre::transport::smtp::Error) -> MailError {
let message = e.to_string();
let code = e.status().and_then(|c| c.to_string().parse::<u16>().ok());
match code {
Some(530 | 534 | 535) => MailError::Unauthorized(message),
Some(code) if e.is_permanent() => MailError::Rejected { code, message },
Some(code) => MailError::Temporary { code, message },
None => MailError::Transport(message),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ports_follow_the_security() {
assert_eq!(SmtpConfig::tls("smtp.example").effective_port(), 465);
assert_eq!(SmtpConfig::starttls("smtp.example").effective_port(), 587);
assert_eq!(
SmtpConfig::plaintext("localhost", 1025).effective_port(),
1025
);
assert_eq!(
SmtpConfig::starttls("smtp.example")
.port(2525)
.effective_port(),
2525
);
}
#[test]
fn the_password_is_never_in_debug() {
let config = SmtpConfig::starttls("smtp.example").credentials("apikey", "hunter2-secret");
let shown = format!("{config:?}");
assert!(!shown.contains("hunter2-secret"));
assert!(shown.contains("<redacted>"));
assert!(shown.contains("apikey"));
let mailer = SmtpMailer::new(config).unwrap();
assert!(!format!("{mailer:?}").contains("hunter2-secret"));
}
#[test]
fn an_empty_host_is_a_config_error() {
assert!(matches!(
SmtpMailer::new(SmtpConfig::tls(" ")),
Err(MailError::Config(_))
));
assert!(SmtpMailer::new(SmtpConfig::tls("smtp.example")).is_ok());
}
#[test]
fn the_pool_keeps_at_least_one_connection() {
let config = SmtpConfig::plaintext("localhost", 1025).pool_max_size(0);
assert!(format!("{config:?}").contains("pool_max_size: 1"));
}
}