feat: read from and reply_to email config params as mailbox addresses

This commit is contained in:
Aravinth Manivannan 2024-05-18 20:14:01 +05:30
parent e90b2d401c
commit 70c4a967bf
Signed by: realaravinth
GPG key ID: F8F50389936984FF
2 changed files with 18 additions and 5 deletions

View file

@ -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 <vanikam@example.com>"
reply_to="Vanikam Support <vanikam@example.com>"

View file

@ -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 <email@address>'";
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 <email@address>'";
let _: Mailbox = s.parse().map_err(|_| ValidationError::new(msg))?;
Ok(())
}
impl Email {
pub fn env_override(mut s: ConfigBuilder<DefaultState>) -> ConfigBuilder<DefaultState> {
for (parameter, env_var_name) in [