From 6f8d5c1eb4643765d6756a869790347825c50d78 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Mon, 3 Jan 2022 18:05:14 +0900 Subject: [PATCH] Add SmtpClient::new_with_addr() method --- Cargo.lock | 1 + plume-models/src/lib.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index d845b0a3..fcc8ed30 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3098,6 +3098,7 @@ dependencies = [ "lettre", "lindera-tantivy", "migrations_internals", + "native-tls", "once_cell", "openssl", "plume-api", diff --git a/plume-models/src/lib.rs b/plume-models/src/lib.rs index 9b7f53e1..1115f83b 100644 --- a/plume-models/src/lib.rs +++ b/plume-models/src/lib.rs @@ -302,6 +302,33 @@ pub fn ap_url(url: &str) -> String { format!("https://{}", url) } +pub trait SmtpNewWithAddr { + fn new_with_addr( + addr: (&str, u16), + ) -> std::result::Result; +} + +impl SmtpNewWithAddr for smtp::SmtpClient { + // Stolen from lettre::smtp::SmtpClient::new_simple() + fn new_with_addr(addr: (&str, u16)) -> std::result::Result { + use native_tls::TlsConnector; + use smtp::{ + client::net::{ClientTlsParameters, DEFAULT_TLS_PROTOCOLS}, + ClientSecurity, SmtpClient, + }; + + let (domain, port) = addr; + + let mut tls_builder = TlsConnector::builder(); + tls_builder.min_protocol_version(Some(DEFAULT_TLS_PROTOCOLS[0])); + + let tls_parameters = + ClientTlsParameters::new(domain.to_string(), tls_builder.build().unwrap()); + + SmtpClient::new((domain, port), ClientSecurity::Wrapper(tls_parameters)) + } +} + #[cfg(test)] #[macro_use] mod tests {