all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Lukas Wagner <l.wagner@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH v2 proxmox 02/11] sys: email: add `forward`
Date: Mon,  2 Oct 2023 10:06:15 +0200	[thread overview]
Message-ID: <20231002080624.198759-3-l.wagner@proxmox.com> (raw)
In-Reply-To: <20231002080624.198759-1-l.wagner@proxmox.com>

This new function forwards an email to new recipients.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
 proxmox-sys/src/email.rs | 52 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)

diff --git a/proxmox-sys/src/email.rs b/proxmox-sys/src/email.rs
index 8b3a1b6..c94f634 100644
--- a/proxmox-sys/src/email.rs
+++ b/proxmox-sys/src/email.rs
@@ -3,7 +3,7 @@
 use std::io::Write;
 use std::process::{Command, Stdio};
 
-use anyhow::{bail, Error};
+use anyhow::{bail, format_err, Error};
 
 /// Sends multi-part mail with text and/or html to a list of recipients
 ///
@@ -110,6 +110,56 @@ pub fn sendmail(
     Ok(())
 }
 
+/// Forwards an email message to a given list of recipients.
+///
+/// ``sendmail`` is used for sending the mail, thus `message` must be
+/// compatible with that (the message is piped into stdin unmodified).
+pub fn forward(
+    mailto: &[&str],
+    mailfrom: &str,
+    message: &[u8],
+    uid: Option<u32>,
+) -> Result<(), Error> {
+    use std::os::unix::process::CommandExt;
+
+    if mailto.is_empty() {
+        bail!("At least one recipient has to be specified!")
+    }
+
+    let mut builder = Command::new("/usr/sbin/sendmail");
+
+    builder
+        .args([
+            "-N", "never", // never send DSN (avoid mail loops)
+            "-f", mailfrom, "--",
+        ])
+        .args(mailto)
+        .stdin(Stdio::piped())
+        .stdout(Stdio::null())
+        .stderr(Stdio::null());
+
+    if let Some(uid) = uid {
+        builder.uid(uid);
+    }
+
+    let mut process = builder
+        .spawn()
+        .map_err(|err| format_err!("could not spawn sendmail process: {err}"))?;
+
+    process
+        .stdin
+        .take()
+        .unwrap()
+        .write_all(message)
+        .map_err(|err| format_err!("couldn't write to sendmail stdin: {err}"))?;
+
+    process
+        .wait()
+        .map_err(|err| format_err!("sendmail did not exit successfully: {err}"))?;
+
+    Ok(())
+}
+
 #[cfg(test)]
 mod test {
     use crate::email::sendmail;
-- 
2.39.2





  parent reply	other threads:[~2023-10-02  8:07 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-02  8:06 [pve-devel] [PATCH v2 many 00/11] notifications: feed system mails into proxmox_notify Lukas Wagner
2023-10-02  8:06 ` [pve-devel] [PATCH v2 debcargo-conf 01/11] package mail-parser 0.8.2 Lukas Wagner
2023-10-20 15:36   ` [pve-devel] applied: " Thomas Lamprecht
2023-10-02  8:06 ` Lukas Wagner [this message]
2023-10-02  8:06 ` [pve-devel] [PATCH v2 proxmox 03/11] notify: introduce Error::Generic Lukas Wagner
2023-10-02  8:06 ` [pve-devel] [PATCH v2 proxmox 04/11] notify: add mechanisms for email message forwarding Lukas Wagner
2023-10-02  8:06 ` [pve-devel] [PATCH v2 proxmox 05/11] notify: add PVE/PBS context Lukas Wagner
2023-10-02  8:06 ` [pve-devel] [PATCH v2 proxmox-perl-rs 06/11] notify: construct Notification via constructor Lukas Wagner
2023-10-02  8:06 ` [pve-devel] [PATCH v2 proxmox-perl-rs 07/11] pve-rs: notify: remove notify_context for PVE Lukas Wagner
2023-10-02  8:06 ` [pve-devel] [PATCH v2 pve-cluster 08/11] datacenter config: add new parameters for system mail forwarding Lukas Wagner
2023-10-02  8:06 ` [pve-devel] [PATCH v2 pve-manager 09/11] ui: notify: add system-mail settings, configuring " Lukas Wagner
2023-10-02  8:06 ` [pve-devel] [PATCH v2 proxmox-mail-forward 10/11] feed forwarded mails into proxmox_notify Lukas Wagner
2023-10-02  8:06 ` [pve-devel] [PATCH v2 pve-docs 11/11] notification: add docs for system mail forwarding Lukas Wagner
2023-10-17  7:28 ` [pve-devel] [PATCH v2 many 00/11] notifications: feed system mails into proxmox_notify Lukas Wagner
2023-10-19  8:30   ` Lukas Wagner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231002080624.198759-3-l.wagner@proxmox.com \
    --to=l.wagner@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal