Skip to content

Commit 90b8f52

Browse files
committed
Better error when the email addresses in the config are invalid
1 parent 5eb08fd commit 90b8f52

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cli/src/util.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,25 @@ pub fn mailer_from_config(
5353
config: &EmailConfig,
5454
templates: &Templates,
5555
) -> Result<Mailer, anyhow::Error> {
56-
let from = config.from.parse()?;
57-
let reply_to = config.reply_to.parse()?;
56+
let from = config
57+
.from
58+
.parse()
59+
.context("invalid email configuration: invalid 'from' address")?;
60+
let reply_to = config
61+
.reply_to
62+
.parse()
63+
.context("invalid email configuration: invalid 'reply_to' address")?;
5864
let transport = match config.transport() {
5965
EmailTransportKind::Blackhole => MailTransport::blackhole(),
6066
EmailTransportKind::Smtp => {
6167
// This should have been set ahead of time
6268
let hostname = config
6369
.hostname()
64-
.context("invalid configuration: missing hostname")?;
70+
.context("invalid email configuration: missing hostname")?;
6571

6672
let mode = config
6773
.mode()
68-
.context("invalid configuration: missing mode")?;
74+
.context("invalid email configuration: missing mode")?;
6975

7076
let credentials = match (config.username(), config.password()) {
7177
(Some(username), Some(password)) => Some(mas_email::SmtpCredentials::new(
@@ -74,7 +80,7 @@ pub fn mailer_from_config(
7480
)),
7581
(None, None) => None,
7682
_ => {
77-
anyhow::bail!("invalid configuration: missing username or password");
83+
anyhow::bail!("invalid email configuration: missing username or password");
7884
}
7985
};
8086

crates/config/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ camino = { workspace = true, features = ["serde1"] }
2222
chrono.workspace = true
2323
figment.workspace = true
2424
ipnetwork = { version = "0.20.0", features = ["serde", "schemars"] }
25+
lettre.workspace = true
2526
schemars.workspace = true
2627
ulid.workspace = true
2728
url.workspace = true

crates/config/src/sections/email.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
#![allow(deprecated)]
88

9-
use std::num::NonZeroU16;
9+
use std::{num::NonZeroU16, str::FromStr};
1010

11+
use lettre::message::Mailbox;
1112
use schemars::JsonSchema;
1213
use serde::{de::Error, Deserialize, Serialize};
1314

@@ -199,6 +200,14 @@ impl ConfigurationSection for EmailConfig {
199200
EmailTransportKind::Blackhole => {}
200201

201202
EmailTransportKind::Smtp => {
203+
if let Err(e) = Mailbox::from_str(&self.from) {
204+
return Err(error_on_field(figment::error::Error::custom(e), "from"));
205+
}
206+
207+
if let Err(e) = Mailbox::from_str(&self.reply_to) {
208+
return Err(error_on_field(figment::error::Error::custom(e), "reply_to"));
209+
}
210+
202211
match (self.username.is_some(), self.password.is_some()) {
203212
(true, true) | (false, false) => {}
204213
(true, false) => {
@@ -237,6 +246,14 @@ impl ConfigurationSection for EmailConfig {
237246
EmailTransportKind::Sendmail => {
238247
let expected_fields = &["from", "reply_to", "transport", "command"];
239248

249+
if let Err(e) = Mailbox::from_str(&self.from) {
250+
return Err(error_on_field(figment::error::Error::custom(e), "from"));
251+
}
252+
253+
if let Err(e) = Mailbox::from_str(&self.reply_to) {
254+
return Err(error_on_field(figment::error::Error::custom(e), "reply_to"));
255+
}
256+
240257
if self.command.is_none() {
241258
return Err(missing_field("command"));
242259
}

0 commit comments

Comments
 (0)