From 2d2745195e8426bd2d63a4ab233c37c04852a356 Mon Sep 17 00:00:00 2001 From: ViViDboarder Date: Fri, 23 Aug 2019 16:22:14 -0700 Subject: [PATCH 1/2] Allow explicitly defined smtp auth mechansim --- .env.template | 1 + src/config.rs | 2 ++ src/mail.rs | 12 ++++++++++++ 3 files changed, 15 insertions(+) diff --git a/.env.template b/.env.template index abd1f6d7..ccd85ce6 100644 --- a/.env.template +++ b/.env.template @@ -149,3 +149,4 @@ # SMTP_SSL=true # SMTP_USERNAME=username # SMTP_PASSWORD=password +# SMTP_AUTH_MECHANISM="Plain" diff --git a/src/config.rs b/src/config.rs index 69620359..5e9789bb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -345,6 +345,8 @@ make_config! { smtp_username: String, true, option; /// Password smtp_password: Pass, true, option; + /// Json form auth mechanism |> Defaults for ssl is "Plain" and "Login" and nothing for non-ssl connections. Possible values: ["Plain", "Login", "Xoauth2"] + smtp_auth_mechanism: String, true, option; }, } diff --git a/src/mail.rs b/src/mail.rs index c729efad..810f7514 100644 --- a/src/mail.rs +++ b/src/mail.rs @@ -1,4 +1,5 @@ use lettre::smtp::authentication::Credentials; +use lettre::smtp::authentication::Mechanism as SmtpAuthMechanism; use lettre::smtp::ConnectionReuseParameters; use lettre::{ClientSecurity, ClientTlsParameters, SmtpClient, SmtpTransport, Transport}; use lettre_email::{EmailBuilder, MimeMultipartType, PartBuilder}; @@ -39,6 +40,17 @@ fn mailer() -> SmtpTransport { _ => smtp_client, }; + let smtp_client = match &CONFIG.smtp_auth_mechanism() { + Some(auth_mechanism_json) => { + let auth_mechanism = serde_json::from_str::(&auth_mechanism_json.clone()); + match auth_mechanism { + Ok(auth_mechanism) => smtp_client.authentication_mechanism(auth_mechanism), + Err(_) => panic!("Failure to parse mechanism. Is it proper Json? Eg. `\"Plain\"` not `Plain`"), + } + }, + _ => smtp_client, + }; + smtp_client .smtp_utf8(true) .connection_reuse(ConnectionReuseParameters::NoReuse) From 672a2455481d52ffc6d0499ec110e0e02269884b Mon Sep 17 00:00:00 2001 From: ViViDboarder Date: Tue, 27 Aug 2019 10:40:38 -0700 Subject: [PATCH 2/2] Remove unecessary clone --- src/mail.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mail.rs b/src/mail.rs index 810f7514..a9edc23f 100644 --- a/src/mail.rs +++ b/src/mail.rs @@ -42,7 +42,7 @@ fn mailer() -> SmtpTransport { let smtp_client = match &CONFIG.smtp_auth_mechanism() { Some(auth_mechanism_json) => { - let auth_mechanism = serde_json::from_str::(&auth_mechanism_json.clone()); + let auth_mechanism = serde_json::from_str::(&auth_mechanism_json); match auth_mechanism { Ok(auth_mechanism) => smtp_client.authentication_mechanism(auth_mechanism), Err(_) => panic!("Failure to parse mechanism. Is it proper Json? Eg. `\"Plain\"` not `Plain`"),