Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions crates/cli/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,25 @@ pub fn mailer_from_config(
config: &EmailConfig,
templates: &Templates,
) -> Result<Mailer, anyhow::Error> {
let from = config.from.parse()?;
let reply_to = config.reply_to.parse()?;
let from = config
.from
.parse()
.context("invalid email configuration: invalid 'from' address")?;
let reply_to = config
.reply_to
.parse()
.context("invalid email configuration: invalid 'reply_to' address")?;
let transport = match config.transport() {
EmailTransportKind::Blackhole => MailTransport::blackhole(),
EmailTransportKind::Smtp => {
// This should have been set ahead of time
let hostname = config
.hostname()
.context("invalid configuration: missing hostname")?;
.context("invalid email configuration: missing hostname")?;

let mode = config
.mode()
.context("invalid configuration: missing mode")?;
.context("invalid email configuration: missing mode")?;

let credentials = match (config.username(), config.password()) {
(Some(username), Some(password)) => Some(mas_email::SmtpCredentials::new(
Expand All @@ -74,7 +80,7 @@ pub fn mailer_from_config(
)),
(None, None) => None,
_ => {
anyhow::bail!("invalid configuration: missing username or password");
anyhow::bail!("invalid email configuration: missing username or password");
}
};

Expand Down
1 change: 1 addition & 0 deletions crates/config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ camino = { workspace = true, features = ["serde1"] }
chrono.workspace = true
figment.workspace = true
ipnetwork = { version = "0.20.0", features = ["serde", "schemars"] }
lettre.workspace = true
schemars.workspace = true
ulid.workspace = true
url.workspace = true
Expand Down
19 changes: 18 additions & 1 deletion crates/config/src/sections/email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

#![allow(deprecated)]

use std::num::NonZeroU16;
use std::{num::NonZeroU16, str::FromStr};

use lettre::message::Mailbox;
use schemars::JsonSchema;
use serde::{de::Error, Deserialize, Serialize};

Expand Down Expand Up @@ -199,6 +200,14 @@ impl ConfigurationSection for EmailConfig {
EmailTransportKind::Blackhole => {}

EmailTransportKind::Smtp => {
if let Err(e) = Mailbox::from_str(&self.from) {
return Err(error_on_field(figment::error::Error::custom(e), "from"));
}

if let Err(e) = Mailbox::from_str(&self.reply_to) {
return Err(error_on_field(figment::error::Error::custom(e), "reply_to"));
}

match (self.username.is_some(), self.password.is_some()) {
(true, true) | (false, false) => {}
(true, false) => {
Expand Down Expand Up @@ -237,6 +246,14 @@ impl ConfigurationSection for EmailConfig {
EmailTransportKind::Sendmail => {
let expected_fields = &["from", "reply_to", "transport", "command"];

if let Err(e) = Mailbox::from_str(&self.from) {
return Err(error_on_field(figment::error::Error::custom(e), "from"));
}

if let Err(e) = Mailbox::from_str(&self.reply_to) {
return Err(error_on_field(figment::error::Error::custom(e), "reply_to"));
}

if self.command.is_none() {
return Err(missing_field("command"));
}
Expand Down
Loading