]> git.proxmox.com Git - proxmox.git/commitdiff
proxmox-sys: imported proxmox/src/tools/email.rs
authorDietmar Maurer <dietmar@proxmox.com>
Tue, 23 Nov 2021 10:06:09 +0000 (11:06 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Wed, 24 Nov 2021 09:00:38 +0000 (10:00 +0100)
Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
proxmox-sys/Cargo.toml
proxmox-sys/src/email.rs [new file with mode: 0644]
proxmox-sys/src/lib.rs
proxmox/src/tools/email.rs [deleted file]
proxmox/src/tools/mod.rs

index 85632cfca131fedffaf155b7a2e17d1449559689..88cbdd1f6bad058e745afd6340626575e0fc8a72 100644 (file)
@@ -23,3 +23,4 @@ proxmox = { path = "../proxmox", version = "0.15", default-features = false }
 proxmox-io = { path = "../proxmox-io", version = "1.0.0" }
 proxmox-lang = { path = "../proxmox-lang", version = "1.0.0" }
 proxmox-borrow = { path = "../proxmox-borrow", version = "1.0.0" }
+proxmox-time = { path = "../proxmox-time", version = "1.0.0" }
diff --git a/proxmox-sys/src/email.rs b/proxmox-sys/src/email.rs
new file mode 100644 (file)
index 0000000..fcdf043
--- /dev/null
@@ -0,0 +1,127 @@
+//! Email related utilities.
+
+use std::io::Write;
+use std::process::{Command, Stdio};
+
+use anyhow::{bail, Error};
+
+/// Sends multi-part mail with text and/or html to a list of recipients
+///
+/// ``sendmail`` is used for sending the mail.
+pub fn sendmail(
+    mailto: &[&str],
+    subject: &str,
+    text: Option<&str>,
+    html: Option<&str>,
+    mailfrom: Option<&str>,
+    author: Option<&str>,
+) -> Result<(), Error> {
+    if mailto.is_empty() {
+        bail!("At least one recipient has to be specified!")
+    }
+    let mailfrom = mailfrom.unwrap_or("root");
+    let recipients = mailto.join(",");
+    let author = author.unwrap_or("Proxmox Backup Server");
+
+    let now = proxmox_time::epoch_i64();
+
+    let mut sendmail_process = match Command::new("/usr/sbin/sendmail")
+        .arg("-B")
+        .arg("8BITMIME")
+        .arg("-f")
+        .arg(mailfrom)
+        .arg("--")
+        .args(mailto)
+        .stdin(Stdio::piped())
+        .spawn()
+    {
+        Err(err) => bail!("could not spawn sendmail process: {}", err),
+        Ok(process) => process,
+    };
+    let mut is_multipart = false;
+    if let (Some(_), Some(_)) = (text, html) {
+        is_multipart = true;
+    }
+
+    let mut body = String::new();
+    let boundary = format!("----_=_NextPart_001_{}", now);
+    if is_multipart {
+        body.push_str("Content-Type: multipart/alternative;\n");
+        body.push_str(&format!("\tboundary=\"{}\"\n", boundary));
+        body.push_str("MIME-Version: 1.0\n");
+    } else if !subject.is_ascii() {
+        body.push_str("MIME-Version: 1.0\n");
+    }
+    if !subject.is_ascii() {
+        body.push_str(&format!(
+            "Subject: =?utf-8?B?{}?=\n",
+            base64::encode(subject)
+        ));
+    } else {
+        body.push_str(&format!("Subject: {}\n", subject));
+    }
+    body.push_str(&format!("From: {} <{}>\n", author, mailfrom));
+    body.push_str(&format!("To: {}\n", &recipients));
+    let localtime = proxmox_time::localtime(now)?;
+    let rfc2822_date = proxmox_time::strftime("%a, %d %b %Y %T %z", &localtime)?;
+    body.push_str(&format!("Date: {}\n", rfc2822_date));
+    if is_multipart {
+        body.push('\n');
+        body.push_str("This is a multi-part message in MIME format.\n");
+        body.push_str(&format!("\n--{}\n", boundary));
+    }
+    if let Some(text) = text {
+        body.push_str("Content-Type: text/plain;\n");
+        body.push_str("\tcharset=\"UTF-8\"\n");
+        body.push_str("Content-Transfer-Encoding: 8bit\n");
+        body.push('\n');
+        body.push_str(text);
+        if is_multipart {
+            body.push_str(&format!("\n--{}\n", boundary));
+        }
+    }
+    if let Some(html) = html {
+        body.push_str("Content-Type: text/html;\n");
+        body.push_str("\tcharset=\"UTF-8\"\n");
+        body.push_str("Content-Transfer-Encoding: 8bit\n");
+        body.push('\n');
+        body.push_str(html);
+        if is_multipart {
+            body.push_str(&format!("\n--{}--", boundary));
+        }
+    }
+
+    if let Err(err) = sendmail_process
+        .stdin
+        .take()
+        .unwrap()
+        .write_all(body.as_bytes())
+    {
+        bail!("couldn't write to sendmail stdin: {}", err)
+    };
+
+    // wait() closes stdin of the child
+    if let Err(err) = sendmail_process.wait() {
+        bail!("sendmail did not exit successfully: {}", err)
+    }
+
+    Ok(())
+}
+
+#[cfg(test)]
+mod test {
+    use crate::tools::email::sendmail;
+
+    #[test]
+    fn email_without_recipients() {
+        let result = sendmail(
+            &[],
+            "Subject2",
+            None,
+            Some("<b>HTML</b>"),
+            None,
+            Some("test1"),
+        );
+        assert!(result.is_err());
+    }
+}
index 2905ab2cb64c02305e387b7acab63e87d7f01ba1..3d0890479ca0078054e299762d694ab8cc9d8062 100644 (file)
@@ -1,5 +1,6 @@
 pub mod command;
 pub mod crypt;
+pub mod email;
 pub mod error;
 pub mod fd;
 pub mod fs;
diff --git a/proxmox/src/tools/email.rs b/proxmox/src/tools/email.rs
deleted file mode 100644 (file)
index fcdf043..0000000
+++ /dev/null
@@ -1,127 +0,0 @@
-//! Email related utilities.
-
-use std::io::Write;
-use std::process::{Command, Stdio};
-
-use anyhow::{bail, Error};
-
-/// Sends multi-part mail with text and/or html to a list of recipients
-///
-/// ``sendmail`` is used for sending the mail.
-pub fn sendmail(
-    mailto: &[&str],
-    subject: &str,
-    text: Option<&str>,
-    html: Option<&str>,
-    mailfrom: Option<&str>,
-    author: Option<&str>,
-) -> Result<(), Error> {
-    if mailto.is_empty() {
-        bail!("At least one recipient has to be specified!")
-    }
-    let mailfrom = mailfrom.unwrap_or("root");
-    let recipients = mailto.join(",");
-    let author = author.unwrap_or("Proxmox Backup Server");
-
-    let now = proxmox_time::epoch_i64();
-
-    let mut sendmail_process = match Command::new("/usr/sbin/sendmail")
-        .arg("-B")
-        .arg("8BITMIME")
-        .arg("-f")
-        .arg(mailfrom)
-        .arg("--")
-        .args(mailto)
-        .stdin(Stdio::piped())
-        .spawn()
-    {
-        Err(err) => bail!("could not spawn sendmail process: {}", err),
-        Ok(process) => process,
-    };
-    let mut is_multipart = false;
-    if let (Some(_), Some(_)) = (text, html) {
-        is_multipart = true;
-    }
-
-    let mut body = String::new();
-    let boundary = format!("----_=_NextPart_001_{}", now);
-    if is_multipart {
-        body.push_str("Content-Type: multipart/alternative;\n");
-        body.push_str(&format!("\tboundary=\"{}\"\n", boundary));
-        body.push_str("MIME-Version: 1.0\n");
-    } else if !subject.is_ascii() {
-        body.push_str("MIME-Version: 1.0\n");
-    }
-    if !subject.is_ascii() {
-        body.push_str(&format!(
-            "Subject: =?utf-8?B?{}?=\n",
-            base64::encode(subject)
-        ));
-    } else {
-        body.push_str(&format!("Subject: {}\n", subject));
-    }
-    body.push_str(&format!("From: {} <{}>\n", author, mailfrom));
-    body.push_str(&format!("To: {}\n", &recipients));
-    let localtime = proxmox_time::localtime(now)?;
-    let rfc2822_date = proxmox_time::strftime("%a, %d %b %Y %T %z", &localtime)?;
-    body.push_str(&format!("Date: {}\n", rfc2822_date));
-    if is_multipart {
-        body.push('\n');
-        body.push_str("This is a multi-part message in MIME format.\n");
-        body.push_str(&format!("\n--{}\n", boundary));
-    }
-    if let Some(text) = text {
-        body.push_str("Content-Type: text/plain;\n");
-        body.push_str("\tcharset=\"UTF-8\"\n");
-        body.push_str("Content-Transfer-Encoding: 8bit\n");
-        body.push('\n');
-        body.push_str(text);
-        if is_multipart {
-            body.push_str(&format!("\n--{}\n", boundary));
-        }
-    }
-    if let Some(html) = html {
-        body.push_str("Content-Type: text/html;\n");
-        body.push_str("\tcharset=\"UTF-8\"\n");
-        body.push_str("Content-Transfer-Encoding: 8bit\n");
-        body.push('\n');
-        body.push_str(html);
-        if is_multipart {
-            body.push_str(&format!("\n--{}--", boundary));
-        }
-    }
-
-    if let Err(err) = sendmail_process
-        .stdin
-        .take()
-        .unwrap()
-        .write_all(body.as_bytes())
-    {
-        bail!("couldn't write to sendmail stdin: {}", err)
-    };
-
-    // wait() closes stdin of the child
-    if let Err(err) = sendmail_process.wait() {
-        bail!("sendmail did not exit successfully: {}", err)
-    }
-
-    Ok(())
-}
-
-#[cfg(test)]
-mod test {
-    use crate::tools::email::sendmail;
-
-    #[test]
-    fn email_without_recipients() {
-        let result = sendmail(
-            &[],
-            "Subject2",
-            None,
-            Some("<b>HTML</b>"),
-            None,
-            Some("test1"),
-        );
-        assert!(result.is_err());
-    }
-}
index 1b94368bcc08c67c1b575e473e47c81e8f09986b..a4a121b02f28911b182690f207fffb77ffc4cb36 100644 (file)
@@ -8,7 +8,6 @@ use lazy_static::lazy_static;
 use proxmox_io::vec;
 
 pub mod common_regex;
-pub mod email;
 pub mod serde;
 pub mod systemd;