From 72159ae8d8a8d9136724d6a7d012cba9ec791767 Mon Sep 17 00:00:00 2001 From: Jacob Rothstein Date: Mon, 15 May 2023 16:35:47 -0700 Subject: [PATCH] use email domain from config --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + src/clients/postmark_client.rs | 7 ++++--- src/config.rs | 5 +++-- src/queue/job/v1/send_invitation_email.rs | 2 +- tests/harness/mod.rs | 2 +- 6 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cf9e2401..1ed448c3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -902,6 +902,7 @@ dependencies = [ "async-lock", "async-session", "base64 0.21.0", + "email_address", "env_logger", "fastrand", "git-version", @@ -959,6 +960,15 @@ version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" +[[package]] +name = "email_address" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2153bd83ebc09db15bcbdc3e2194d901804952e3dc96967e1cd3b0c5c32d112" +dependencies = [ + "serde", +] + [[package]] name = "encoding_rs" version = "0.8.32" diff --git a/Cargo.toml b/Cargo.toml index ef2b32fe..fa83b3f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ members = [".", "migration"] async-lock = "2.7.0" async-session = "3.0.0" base64 = "0.21.0" +email_address = "0.2.4" env_logger = "0.10.0" fastrand = "1.9.0" git-version = "0.3.5" diff --git a/src/clients/postmark_client.rs b/src/clients/postmark_client.rs index 6db2c9d0..3dbb928a 100644 --- a/src/clients/postmark_client.rs +++ b/src/clients/postmark_client.rs @@ -2,6 +2,7 @@ use crate::{ clients::{ClientConnExt, ClientError}, ApiConfig, }; +use email_address::EmailAddress; use serde::{de::DeserializeOwned, Deserialize, Serialize}; use serde_json::{json, Value}; use trillium::{async_trait, Conn, KnownHeaderName}; @@ -13,7 +14,7 @@ use url::Url; pub struct PostmarkClient { token: String, client: Client, - email: String, + email: EmailAddress, base_url: Url, } @@ -39,7 +40,7 @@ impl PostmarkClient { email .as_object_mut() .unwrap() - .insert("From".into(), self.email.clone().into()); + .insert("From".into(), self.email.to_string().into()); self.post("/email", &email).await } @@ -53,7 +54,7 @@ impl PostmarkClient { let headers = match message_id { Some(m) => json!([{ "Name": "Message-ID", - "Value": format!("<{m}>") + "Value": format!("<{m}@{}>", self.email.domain()) }]), None => json!([]), }; diff --git a/src/config.rs b/src/config.rs index d44fa709..9b29ab58 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,5 @@ use crate::handler::oauth2::Oauth2Config; +use email_address::EmailAddress; use std::{env::VarError, str::FromStr}; use thiserror::Error; use trillium_client::Client; @@ -22,7 +23,7 @@ pub struct ApiConfig { pub prometheus_host: String, pub prometheus_port: u16, pub postmark_token: String, - pub email_address: String, + pub email_address: EmailAddress, pub postmark_url: Url, pub client: Client, } @@ -91,7 +92,7 @@ impl ApiConfig { "16-bit number", )?, postmark_token: var("POSTMARK_TOKEN", "string")?, - email_address: var("EMAIL_ADDRESS", "string")?, + email_address: var("EMAIL_ADDRESS", "email")?, postmark_url: Url::parse("https://api.postmarkapp.com").unwrap(), client: Client::new(RustlsConfig::default().with_tcp_config(ClientConfig::default())) .with_default_pool(), diff --git a/src/queue/job/v1/send_invitation_email.rs b/src/queue/job/v1/send_invitation_email.rs index 33f7a293..38d9ff86 100644 --- a/src/queue/job/v1/send_invitation_email.rs +++ b/src/queue/job/v1/send_invitation_email.rs @@ -43,7 +43,7 @@ impl SendInvitationEmail { "account_name": &account.name, "action_url": self.action_url }), - Some(format!("{}@divviup.org", self.message_id)), + Some(self.message_id.to_string()), ) .await?; diff --git a/tests/harness/mod.rs b/tests/harness/mod.rs index 68f95d9c..b15f6949 100644 --- a/tests/harness/mod.rs +++ b/tests/harness/mod.rs @@ -68,7 +68,7 @@ pub fn config(api_mocks: impl Handler) -> ApiConfig { prometheus_host: "localhost".into(), prometheus_port: 9464, postmark_token: "-".into(), - email_address: "test@example.test".into(), + email_address: "test@example.test".parse().unwrap(), postmark_url: POSTMARK_URL.parse().unwrap(), client: Client::new(trillium_testing::connector(api_mocks)), }