Adding starttls support. Fixes #1997

starttls_smtp
Dessalines 2022-01-20 10:19:06 -05:00
parent f23fed70bc
commit 56642efeda
3 changed files with 20 additions and 15 deletions

View File

@ -76,8 +76,8 @@
smtp_password: "string" smtp_password: "string"
# Address to send emails from, eg noreply@your-instance.com # Address to send emails from, eg noreply@your-instance.com
smtp_from_address: "noreply@example.com" smtp_from_address: "noreply@example.com"
# Whether or not smtp connections should use tls # Whether or not smtp connections should use tls. Can be missing (IE None), tls, or starttls
use_tls: true use_tls: "string"
} }
# Parameters for automatic configuration of new instance (only used at first start) # Parameters for automatic configuration of new instance (only used at first start)
setup: { setup: {

View File

@ -1,11 +1,7 @@
use crate::{settings::structs::Settings, LemmyError}; use crate::{settings::structs::Settings, LemmyError};
use lettre::{ use lettre::{
message::{header, Mailbox, MultiPart, SinglePart}, message::{header, Mailbox, MultiPart, SinglePart},
transport::smtp::{ transport::smtp::{authentication::Credentials, extension::ClientId},
authentication::Credentials,
client::{Tls, TlsParameters},
extension::ClientId,
},
Address, Address,
Message, Message,
SmtpTransport, SmtpTransport,
@ -77,13 +73,21 @@ pub fn send_email(
// don't worry about 'dangeous'. it's just that leaving it at the default configuration // don't worry about 'dangeous'. it's just that leaving it at the default configuration
// is bad. // is bad.
let mut builder = SmtpTransport::builder_dangerous(smtp_server).port(smtp_port);
// Set the TLS // Set the TLS
if email_config.use_tls { let builder_dangerous = SmtpTransport::builder_dangerous(smtp_server).port(smtp_port);
let tls_config = TlsParameters::new(smtp_server.to_string()).expect("the TLS backend is happy");
builder = builder.tls(Tls::Wrapper(tls_config)); let mut builder = if let Some(tls_type) = email_config.use_tls {
} if tls_type == "starttls" {
SmtpTransport::starttls_relay(smtp_server)?
} else if tls_type == "tls" {
SmtpTransport::relay(smtp_server)?
} else {
builder_dangerous
}
} else {
builder_dangerous
};
// Set the creds if they exist // Set the creds if they exist
if let (Some(username), Some(password)) = (email_config.smtp_login, email_config.smtp_password) { if let (Some(username), Some(password)) = (email_config.smtp_login, email_config.smtp_password) {

View File

@ -89,7 +89,7 @@ pub struct DatabaseConfig {
pub pool_size: u32, pub pool_size: u32,
} }
#[derive(Debug, Deserialize, Serialize, Clone, Document)] #[derive(Debug, Deserialize, Serialize, Clone, Document, SmartDefault)]
pub struct EmailConfig { pub struct EmailConfig {
/// Hostname and port of the smtp server /// Hostname and port of the smtp server
#[doku(example = "localhost:25")] #[doku(example = "localhost:25")]
@ -101,8 +101,9 @@ pub struct EmailConfig {
#[doku(example = "noreply@example.com")] #[doku(example = "noreply@example.com")]
/// Address to send emails from, eg "noreply@your-instance.com" /// Address to send emails from, eg "noreply@your-instance.com"
pub smtp_from_address: String, pub smtp_from_address: String,
/// Whether or not smtp connections should use tls /// Whether or not smtp connections should use tls. Can be missing (IE None), tls, or starttls
pub use_tls: bool, #[default(None)]
pub use_tls: Option<String>,
} }
#[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)] #[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)]