From 3899ec9c031e4c843c927877312a7e39b7dffbbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20DOUIN?= Date: Sun, 6 Mar 2022 09:51:34 +0100 Subject: [PATCH] build smtp envelope after executing pre_send hook --- src/smtp/smtp_service.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/smtp/smtp_service.rs b/src/smtp/smtp_service.rs index 35d79bd9..13ea6cec 100644 --- a/src/smtp/smtp_service.rs +++ b/src/smtp/smtp_service.rs @@ -56,18 +56,22 @@ impl LettreService<'_> { impl SmtpService for LettreService<'_> { fn send(&mut self, account: &AccountConfig, msg: &Msg) -> Result> { - let envelope: lettre::address::Envelope = msg.try_into()?; - let mut msg = msg.into_sendable_msg(account)?.formatted(); + let mut raw_msg = msg.into_sendable_msg(account)?.formatted(); - if let Some(cmd) = account.hooks.pre_send.as_deref() { - for cmd in cmd.split('|') { - msg = pipe_cmd(cmd.trim(), &msg) - .with_context(|| format!("cannot execute pre-send hook {:?}", cmd))? - } - }; + let envelope: lettre::address::Envelope = + if let Some(cmd) = account.hooks.pre_send.as_deref() { + for cmd in cmd.split('|') { + raw_msg = pipe_cmd(cmd.trim(), &raw_msg) + .with_context(|| format!("cannot execute pre-send hook {:?}", cmd))? + } + let parsed_mail = mailparse::parse_mail(&raw_msg)?; + Msg::from_parsed_mail(parsed_mail, account)?.try_into() + } else { + msg.try_into() + }?; - self.transport()?.send_raw(&envelope, &msg)?; - Ok(msg) + self.transport()?.send_raw(&envelope, &raw_msg)?; + Ok(raw_msg) } }