fix: read email config as a URL for optionally disabling TLS

This commit is contained in:
Aravinth Manivannan 2024-05-18 20:41:27 +05:30
parent 70c4a967bf
commit d5bcd3493b
Signed by: realaravinth
GPG key ID: F8F50389936984FF
3 changed files with 16 additions and 24 deletions

View file

@ -1 +1,9 @@
export POSTGRES_DATABASE_URL="postgres://postgres:password@localhost:5432/postgres"
# development maildev/maildev container creds
export VANIKAM_email_USERNAME=admin
export VANIKAM_email_PASSWORD=password
export VANIKAM_email_SERVER_HOSTNAME=localhost:10025
export MAILDEV_URL=http://localhost:1080
export VANIKAM_email_FROM="Vanikam Info <info@vanikam.app>"
export VANIKAM_email_REPLY_TO="Vanikam Support <support@vanikam.app>"

View file

@ -26,8 +26,6 @@ url = "postgres://example.org" # hack for tests to run successfully
pool = 4
[email]
username="vanikam_mailer"
password="vanikam_mailer_password"
server_hostname="smtp.vanikam.example.com"
url="smtps://username:password@smtp.example.com:465"
from="Vanikam Info <vanikam@example.com>"
reply_to="Vanikam Support <vanikam@example.com>"

View file

@ -7,13 +7,12 @@ use std::env;
use config::{builder::DefaultState, ConfigBuilder};
use lettre::message::Mailbox;
use serde::{Deserialize, Serialize};
use url::Url;
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,
pub url: Url,
#[validate(custom(function = "from_address_validation"))]
pub from: String,
#[validate(custom(function = "reply_to_address_validation"))]
@ -35,9 +34,7 @@ fn reply_to_address_validation(s: &str) -> Result<(), ValidationError> {
impl Email {
pub fn env_override(mut s: ConfigBuilder<DefaultState>) -> ConfigBuilder<DefaultState> {
for (parameter, env_var_name) in [
("email.username", "VANIKAM_email_USERNAME"),
("email.password", "VANIKAM_email_PASSWORD"),
("email.server_hostname", "VANIKAM_email_SERVER_HOSTNAME"),
("email.url", "VANIKAM_email_URL"),
("email.from", "VANIKAM_email_FROM"),
("email.reply_to", "VANIKAM_email_REPLY_TO"),
]
@ -62,17 +59,12 @@ mod tests {
#[test]
fn test_db_env_override() {
let init_settings = crate::settings::Settings::new().unwrap();
env_helper!(
init_settings,
"VANIKAM_email_USERNAME",
"email_username",
email.username
);
env_helper!(
init_settings,
"VANIKAM_email_PASSWORD",
"email_password",
email.password
"VANIKAM_email_URL",
Url::parse("smtp://smtp.example.org:465").unwrap(),
email.url
);
env_helper!(
init_settings,
@ -86,11 +78,5 @@ mod tests {
"reploy_to@example.com",
email.reply_to
);
env_helper!(
init_settings,
"VANIKAM_email_SERVER_HOSTNAME",
"smtp.example.com",
email.server_hostname
);
}
}