notify: sendmail: query default author/mailfrom from context

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
This commit is contained in:
Lukas Wagner 2023-07-20 16:31:46 +02:00 committed by Wolfgang Bumiller
parent c5f91aa1c8
commit da2e7b8da9
2 changed files with 15 additions and 5 deletions

View File

@ -3,6 +3,8 @@ use std::fmt::Debug;
pub trait Context: Send + Sync + Debug {
fn lookup_email_for_user(&self, user: &str) -> Option<String>;
fn default_sendmail_author(&self) -> String;
fn default_sendmail_from(&self) -> String;
}
static CONTEXT: OnceCell<&'static dyn Context> = OnceCell::new();

View File

@ -112,9 +112,17 @@ impl Endpoint for SendmailEndpoint {
let text_part =
renderer::render_template(TemplateRenderer::Plaintext, &notification.body, properties)?;
// proxmox_sys::email::sendmail will set the author to
// "Proxmox Backup Server" if it is not set.
let author = self.config.author.as_deref().or(Some(""));
let author = self
.config
.author
.clone()
.unwrap_or_else(|| context().default_sendmail_author());
let mailfrom = self
.config
.from_address
.clone()
.unwrap_or_else(|| context().default_sendmail_from());
let recipients_str: Vec<&str> = recipients.iter().map(String::as_str).collect();
@ -123,8 +131,8 @@ impl Endpoint for SendmailEndpoint {
&subject,
Some(&text_part),
Some(&html_part),
self.config.from_address.as_deref(),
author,
Some(&mailfrom),
Some(&author),
)
.map_err(|err| Error::NotifyFailed(self.config.name.clone(), err.into()))
}