From 70c4a967bf31cdf9a29f4b8b3e41299831b2523d Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Sat, 18 May 2024 20:14:01 +0530 Subject: [PATCH] feat: read from and reply_to email config params as mailbox addresses --- config/default.toml | 4 ++-- src/settings/email.rs | 19 ++++++++++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/config/default.toml b/config/default.toml index 600f2bd..916abdb 100644 --- a/config/default.toml +++ b/config/default.toml @@ -29,5 +29,5 @@ pool = 4 username="vanikam_mailer" password="vanikam_mailer_password" server_hostname="smtp.vanikam.example.com" -from="vanikam@example.com" -reply_to="vanikam@example.com" +from="Vanikam Info " +reply_to="Vanikam Support " diff --git a/src/settings/email.rs b/src/settings/email.rs index 58c88e6..2b3aeae 100644 --- a/src/settings/email.rs +++ b/src/settings/email.rs @@ -5,20 +5,33 @@ use std::env; use config::{builder::DefaultState, ConfigBuilder}; +use lettre::message::Mailbox; use serde::{Deserialize, Serialize}; -use validator::Validate; +use validator::{Validate, ValidationError}; #[derive(Debug, Clone, Serialize, Deserialize, Validate, Eq, PartialEq)] pub struct Email { pub username: String, pub password: String, pub server_hostname: String, - #[validate(email)] + #[validate(custom(function = "from_address_validation"))] pub from: String, - #[validate(email)] + #[validate(custom(function = "reply_to_address_validation"))] pub reply_to: String, } +fn from_address_validation(s: &str) -> Result<(), ValidationError> { + let msg = "from address must be of format 'Name '"; + let _: Mailbox = s.parse().map_err(|_| ValidationError::new(msg))?; + Ok(()) +} + +fn reply_to_address_validation(s: &str) -> Result<(), ValidationError> { + let msg = "to address must be of format 'Name '"; + let _: Mailbox = s.parse().map_err(|_| ValidationError::new(msg))?; + Ok(()) +} + impl Email { pub fn env_override(mut s: ConfigBuilder) -> ConfigBuilder { for (parameter, env_var_name) in [