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 BC8681FF38F for ; Mon, 24 Jun 2024 14:31:38 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 1341436434; Mon, 24 Jun 2024 14:31:41 +0200 (CEST) From: Lukas Wagner To: pve-devel@lists.proxmox.com Date: Mon, 24 Jun 2024 14:31:34 +0200 Message-Id: <20240624123134.321417-6-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 6/6] notify: sendmail: code style improvements 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" No functional changes intended. Signed-off-by: Lukas Wagner --- proxmox-notify/src/endpoints/sendmail.rs | 57 +++++++++++------------- 1 file changed, 25 insertions(+), 32 deletions(-) diff --git a/proxmox-notify/src/endpoints/sendmail.rs b/proxmox-notify/src/endpoints/sendmail.rs index c28d9211..42b2d3a8 100644 --- a/proxmox-notify/src/endpoints/sendmail.rs +++ b/proxmox-notify/src/endpoints/sendmail.rs @@ -185,7 +185,7 @@ fn sendmail( let now = proxmox_time::epoch_i64(); let body = format_mail(mailto, mailfrom, author, subject, text, html, now)?; - let mut sendmail_process = match Command::new("/usr/sbin/sendmail") + let mut sendmail_process = Command::new("/usr/sbin/sendmail") .arg("-B") .arg("8BITMIME") .arg("-f") @@ -194,32 +194,18 @@ fn sendmail( .args(mailto) .stdin(Stdio::piped()) .spawn() - { - Err(err) => { - return Err(Error::Generic(format!( - "could not spawn sendmail process: {err}" - ))) - } - Ok(process) => process, - }; + .map_err(|err| Error::Generic(format!("could not spawn sendmail process: {err}")))?; - if let Err(err) = sendmail_process + sendmail_process .stdin .take() - .unwrap() + .expect("stdin already taken") .write_all(body.as_bytes()) - { - return Err(Error::Generic(format!( - "couldn't write to sendmail stdin: {err}" - ))); - }; - - // wait() closes stdin of the child - if let Err(err) = sendmail_process.wait() { - return Err(Error::Generic(format!( - "sendmail did not exit successfully: {err}" - ))); - } + .map_err(|err| Error::Generic(format!("couldn't write to sendmail stdin: {err}")))?; + + sendmail_process + .wait() + .map_err(|err| Error::Generic(format!("sendmail did not exit successfully: {err}")))?; Ok(()) } @@ -236,39 +222,46 @@ fn format_mail( use std::fmt::Write as _; let recipients = mailto.join(","); + let boundary = format!("----_=_NextPart_001_{timestamp}"); + let mut body = String::new(); - let boundary = format!("----_=_NextPart_001_{}", timestamp); + // Format email header body.push_str("Content-Type: multipart/alternative;\n"); - let _ = writeln!(body, "\tboundary=\"{}\"", boundary); + 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 { - let _ = writeln!(body, "Subject: {}", subject); + let _ = writeln!(body, "Subject: {subject}"); } - let _ = writeln!(body, "From: {} <{}>", author, mailfrom); - let _ = writeln!(body, "To: {}", &recipients); + let _ = writeln!(body, "From: {author} <{mailfrom}>"); + let _ = writeln!(body, "To: {recipients}"); let rfc2822_date = proxmox_time::epoch_to_rfc2822(timestamp) .map_err(|err| Error::Generic(format!("failed to format time: {err}")))?; - let _ = writeln!(body, "Date: {}", rfc2822_date); + let _ = writeln!(body, "Date: {rfc2822_date}"); body.push_str("Auto-Submitted: auto-generated;\n"); body.push('\n'); + + // Format email body body.push_str("This is a multi-part message in MIME format.\n"); - let _ = write!(body, "\n--{}\n", boundary); + let _ = write!(body, "\n--{boundary}\n"); + 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); + let _ = write!(body, "\n--{boundary}\n"); + 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); + let _ = write!(body, "\n--{boundary}--"); + Ok(body) } -- 2.39.2 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel