From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id B1B4F1FF38F for ; Mon, 24 Jun 2024 14:32:13 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 81566363E3; Mon, 24 Jun 2024 14:32:11 +0200 (CEST) From: Lukas Wagner To: pve-devel@lists.proxmox.com Date: Mon, 24 Jun 2024 14:31:33 +0200 Message-Id: <20240624123134.321417-5-l.wagner@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240624123134.321417-1-l.wagner@proxmox.com> References: <20240624123134.321417-1-l.wagner@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.005 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pve-devel] [PATCH proxmox 5/6] notify: sendmail: always send multi-part message X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox VE development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" Even if we don't have an HTML template available, we always send an HTML part (the plain text part wrapped in
) to
improve rendering in certain mail clients. This means
we can simply message formatting, since we do not have to
distinguish between single-part and multi-part messages.

Signed-off-by: Lukas Wagner 
---
 proxmox-notify/src/endpoints/sendmail.rs | 81 +++++++++---------------
 1 file changed, 29 insertions(+), 52 deletions(-)

diff --git a/proxmox-notify/src/endpoints/sendmail.rs b/proxmox-notify/src/endpoints/sendmail.rs
index 241a2578..c28d9211 100644
--- a/proxmox-notify/src/endpoints/sendmail.rs
+++ b/proxmox-notify/src/endpoints/sendmail.rs
@@ -139,8 +139,8 @@ impl Endpoint for SendmailEndpoint {
                 sendmail(
                     &recipients_str,
                     &subject,
-                    Some(&text_part),
-                    Some(&html_part),
+                    &text_part,
+                    &html_part,
                     &mailfrom,
                     &author,
                 )
@@ -172,8 +172,8 @@ impl Endpoint for SendmailEndpoint {
 fn sendmail(
     mailto: &[&str],
     subject: &str,
-    text: Option<&str>,
-    html: Option<&str>,
+    text: &str,
+    html: &str,
     mailfrom: &str,
     author: &str,
 ) -> Result<(), Error> {
@@ -229,26 +229,20 @@ fn format_mail(
     mailfrom: &str,
     author: &str,
     subject: &str,
-    text: Option<&str>,
-    html: Option<&str>,
+    text: &str,
+    html: &str,
     timestamp: i64,
 ) -> Result {
     use std::fmt::Write as _;
 
     let recipients = mailto.join(",");
-    let mut is_multipart = false;
-    if let (Some(_), Some(_)) = (text, html) {
-        is_multipart = true;
-    }
     let mut body = String::new();
+
     let boundary = format!("----_=_NextPart_001_{}", timestamp);
-    if is_multipart {
-        body.push_str("Content-Type: multipart/alternative;\n");
-        let _ = writeln!(body, "\tboundary=\"{}\"", boundary);
-        body.push_str("MIME-Version: 1.0\n");
-    } else if !subject.is_ascii() {
-        body.push_str("MIME-Version: 1.0\n");
-    }
+    body.push_str("Content-Type: multipart/alternative;\n");
+    let _ = writeln!(body, "\tboundary=\"{}\"", boundary);
+    body.push_str("MIME-Version: 1.0\n");
+
     if !subject.is_ascii() {
         let _ = writeln!(body, "Subject: =?utf-8?B?{}?=", base64::encode(subject));
     } else {
@@ -260,31 +254,21 @@ fn format_mail(
         .map_err(|err| Error::Generic(format!("failed to format time: {err}")))?;
     let _ = writeln!(body, "Date: {}", rfc2822_date);
     body.push_str("Auto-Submitted: auto-generated;\n");
-    if is_multipart {
-        body.push('\n');
-        body.push_str("This is a multi-part message in MIME format.\n");
-        let _ = write!(body, "\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 {
-            let _ = write!(body, "\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 {
-            let _ = write!(body, "\n--{}--", boundary);
-        }
-    }
+    body.push('\n');
+    body.push_str("This is a multi-part message in MIME format.\n");
+    let _ = write!(body, "\n--{}\n", boundary);
+    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);
+    let _ = write!(body, "\n--{}\n", boundary);
+    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);
+    let _ = write!(body, "\n--{}--", boundary);
     Ok(body)
 }
 
@@ -342,14 +326,7 @@ mod test {
 
     #[test]
     fn email_without_recipients() {
-        let result = sendmail(
-            &[],
-            "Subject2",
-            None,
-            Some("HTML"),
-            "root",
-            "Proxmox",
-        );
+        let result = sendmail(&[], "Subject2", "", "HTML", "root", "Proxmox");
         assert!(result.is_err());
     }
 
@@ -360,8 +337,8 @@ mod test {
             "foobar@example.com",
             "Fred Oobar",
             "This is the subject",
-            Some("This is the plain body"),
-            Some("This is the HTML body"),
+            "This is the plain body",
+            "This is the HTML body",
             1718977850,
         )
         .expect("format_message failed");
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel