Skip to content

Commit 4a682ec

Browse files
committed
handle smtp user & pass as trulty optional
1 parent 9e06efa commit 4a682ec

2 files changed

Lines changed: 11 additions & 12 deletions

File tree

crates/defguard_mail/src/lib.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,28 @@ pub(crate) struct SmtpSettings {
2121
server: String,
2222
port: u16,
2323
encryption: SmtpEncryption,
24-
user: String,
25-
password: String,
24+
user: Option<String>,
25+
password: Option<String>,
2626
sender: String,
2727
}
2828

2929
impl SmtpSettings {
3030
/// Constructs `SmtpSettings` from `Settings`. Returns error if `SmtpSettings` are incomplete.
3131
pub(crate) fn from_settings(settings: Settings) -> Result<SmtpSettings, MailError> {
32-
if let (Some(server), Some(port), encryption, Some(user), Some(password), Some(sender)) = (
32+
if let (Some(server), Some(port), Some(sender)) = (
3333
settings.smtp_server,
3434
settings.smtp_port,
35-
settings.smtp_encryption,
36-
settings.smtp_user,
37-
settings.smtp_password,
3835
settings.smtp_sender,
3936
) {
4037
let port = port.try_into().map_err(|_| MailError::InvalidPort(port))?;
4138
Ok(Self {
4239
server,
4340
port,
44-
encryption,
45-
user,
46-
password: password.expose_secret().to_string(),
41+
encryption: settings.smtp_encryption,
42+
user: settings.smtp_user,
43+
password: settings
44+
.smtp_password
45+
.map(|p| p.expose_secret().to_string()),
4746
sender,
4847
})
4948
} else {

crates/defguard_mail/src/mail.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,11 @@ impl Mail {
260260
.timeout(Some(SMTP_TIMEOUT));
261261

262262
// Skip credentials if any of them is empty
263-
let builder = if settings.user.is_empty() || settings.password.is_empty() {
263+
let builder = if let (Some(user), Some(password)) = (settings.user, settings.password) {
264+
builder.credentials(Credentials::new(user, password))
265+
} else {
264266
debug!("SMTP credentials were not provided, skipping username/password authentication");
265267
builder
266-
} else {
267-
builder.credentials(Credentials::new(settings.user, settings.password))
268268
};
269269

270270
Ok(builder.build())

0 commit comments

Comments
 (0)