public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Shannon Sterz <s.sterz@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox 2/3] sendmail: encode non-ascii bodies as base64 to improve comptability
Date: Tue,  2 Sep 2025 16:27:16 +0200	[thread overview]
Message-ID: <20250902142720.371671-3-s.sterz@proxmox.com> (raw)
In-Reply-To: <20250902142720.371671-1-s.sterz@proxmox.com>

and use 7bit encoding otherwise. this should further improve
compatability with older mail servers that don't really handle 8bit
utf8 bodies properly. by base64 encoding the bodies that require it
and declaring 7bit ascii compatability otherwise, this should work
around compatability constraints.

Signed-off-by: Shannon Sterz <s.sterz@proxmox.com>
---
 proxmox-sendmail/src/lib.rs | 111 ++++++++++++++++++++----------------
 1 file changed, 62 insertions(+), 49 deletions(-)

diff --git a/proxmox-sendmail/src/lib.rs b/proxmox-sendmail/src/lib.rs
index ebbcfe86..fb201007 100644
--- a/proxmox-sendmail/src/lib.rs
+++ b/proxmox-sendmail/src/lib.rs
@@ -41,6 +41,24 @@ const RFC5987SET: &AsciiSet = &CONTROLS
     .add(b'{')
     .add(b'}');
 
+// base64 encode and hard-wrap the base64 encoded string every 72 characters. this improves
+// compatibility.
+fn encode_base64_formatted<T: AsRef<[u8]>>(raw: T) -> String {
+    proxmox_base64::encode(raw)
+        .chars()
+        .enumerate()
+        .flat_map(|(i, c)| {
+            if i != 0 && i % 72 == 0 {
+                Some('\n')
+            } else {
+                None
+            }
+            .into_iter()
+            .chain(std::iter::once(c))
+        })
+        .collect::<String>()
+}
+
 struct Recipient {
     name: Option<String>,
     email: String,
@@ -100,25 +118,9 @@ impl Attachment<'_> {
             "Content-Disposition: attachment; filename=\"{encoded_filename}\"; filename*=UTF-8''{}",
             utf8_percent_encode(&self.filename, RFC5987SET)
         );
-        attachment.push_str("Content-Transfer-Encoding: base64\n\n");
 
-        // base64 encode the attachment and hard-wrap the base64 encoded string every 72
-        // characters. this improves compatability.
-        attachment.push_str(
-            &proxmox_base64::encode(self.content)
-                .chars()
-                .enumerate()
-                .flat_map(|(i, c)| {
-                    if i != 0 && i % 72 == 0 {
-                        Some('\n')
-                    } else {
-                        None
-                    }
-                    .into_iter()
-                    .chain(std::iter::once(c))
-                })
-                .collect::<String>(),
-        );
+        attachment.push_str("Content-Transfer-Encoding: base64\n\n");
+        attachment.push_str(&encode_base64_formatted(self.content));
 
         attachment
     }
@@ -400,7 +402,11 @@ impl<'a> Mail<'a> {
             header.push_str("Content-Type: multipart/alternative;\n");
             writeln!(header, "\tboundary=\"{html_boundary}\"")?;
             header.push_str("MIME-Version: 1.0\n");
-        } else if !self.subject.is_ascii() || !self.mail_author.is_ascii() || encoded_to {
+        } else if !self.subject.is_ascii()
+            || !self.mail_author.is_ascii()
+            || !self.body_txt.is_ascii()
+            || encoded_to
+        {
             header.push_str("MIME-Version: 1.0\n");
         }
 
@@ -475,15 +481,28 @@ impl<'a> Mail<'a> {
 
         body.push_str("Content-Type: text/plain;\n");
         body.push_str("\tcharset=\"UTF-8\"\n");
-        body.push_str("Content-Transfer-Encoding: 8bit\n\n");
-        body.push_str(&self.body_txt);
+
+        if self.body_txt.is_ascii() {
+            body.push_str("Content-Transfer-Encoding: 7bit\n\n");
+            body.push_str(&self.body_txt);
+        } else {
+            body.push_str("Content-Transfer-Encoding: base64\n\n");
+            body.push_str(&encode_base64_formatted(&self.body_txt));
+        }
 
         if let Some(html) = &self.body_html {
             writeln!(body, "\n--{html_boundary}")?;
             body.push_str("Content-Type: text/html;\n");
             body.push_str("\tcharset=\"UTF-8\"\n");
-            body.push_str("Content-Transfer-Encoding: 8bit\n\n");
-            body.push_str(html);
+
+            if html.is_ascii() {
+                body.push_str("Content-Transfer-Encoding: 7bit\n\n");
+                body.push_str(html);
+            } else {
+                body.push_str("Content-Transfer-Encoding: base64\n\n");
+                body.push_str(&encode_base64_formatted(html));
+            }
+
             write!(body, "\n--{html_boundary}--")?;
         }
 
@@ -547,7 +566,7 @@ Date: Thu, 01 Jan 1970 01:00:00 +0100
 Auto-Submitted: auto-generated;
 Content-Type: text/plain;
 	charset="UTF-8"
-Content-Transfer-Encoding: 8bit
+Content-Transfer-Encoding: 7bit
 
 This is just ascii text.
 Nothing too special."#,
@@ -577,7 +596,7 @@ Date: Thu, 01 Jan 1970 01:00:00 +0100
 Auto-Submitted: auto-generated;
 Content-Type: text/plain;
 	charset="UTF-8"
-Content-Transfer-Encoding: 8bit
+Content-Transfer-Encoding: 7bit
 
 This is just ascii text.
 Nothing too special."#,
@@ -609,7 +628,7 @@ Date: Thu, 01 Jan 1970 01:00:00 +0100
 Auto-Submitted: auto-generated;
 Content-Type: text/plain;
 	charset="UTF-8"
-Content-Transfer-Encoding: 8bit
+Content-Transfer-Encoding: 7bit
 
 This is just ascii text.
 Nothing too special."#,
@@ -638,12 +657,10 @@ Date: Thu, 28 Nov 2024 16:04:11 +0100
 Auto-Submitted: auto-generated;
 Content-Type: text/plain;
 	charset="UTF-8"
-Content-Transfer-Encoding: 8bit
+Content-Transfer-Encoding: base64
 
-This utf-8 email should handle emojis
-🧑📧
-and weird german characters: öäüß
-and more."#,
+VGhpcyB1dGYtOCBlbWFpbCBzaG91bGQgaGFuZGxlIGVtb2ppcwrwn6eR8J+TpwphbmQgd2Vp
+cmQgZ2VybWFuIGNoYXJhY3RlcnM6IMO2w6TDvMOfCmFuZCBtb3JlLg=="#,
         )
     }
 
@@ -674,14 +691,14 @@ This is a multi-part message in MIME format.
 ------_=_NextPart_002_1732806251
 Content-Type: text/plain;
 	charset="UTF-8"
-Content-Transfer-Encoding: 8bit
+Content-Transfer-Encoding: 7bit
 
 Lorem Ipsum Dolor Sit
 Amet
 ------_=_NextPart_002_1732806251
 Content-Type: text/html;
 	charset="UTF-8"
-Content-Transfer-Encoding: 8bit
+Content-Transfer-Encoding: 7bit
 
 <html lang="de-at"><head></head><body>
 	<pre>
@@ -728,7 +745,7 @@ This is a multi-part message in MIME format.
 ------_=_NextPart_001_1732806251
 Content-Type: text/plain;
 	charset="UTF-8"
-Content-Transfer-Encoding: 8bit
+Content-Transfer-Encoding: 7bit
 
 Lorem Ipsum Dolor Sit
 Amet
@@ -786,14 +803,14 @@ MIME-Version: 1.0
 ------_=_NextPart_002_1732806251
 Content-Type: text/plain;
 	charset="UTF-8"
-Content-Transfer-Encoding: 8bit
+Content-Transfer-Encoding: 7bit
 
 Lorem Ipsum Dolor Sit
 Amet
 ------_=_NextPart_002_1732806251
 Content-Type: text/html;
 	charset="UTF-8"
-Content-Transfer-Encoding: 8bit
+Content-Transfer-Encoding: 7bit
 
 <html lang="de-at"><head></head><body>
 	<pre>
@@ -848,13 +865,13 @@ This is a multi-part message in MIME format.
 ------_=_NextPart_002_1718977850
 Content-Type: text/plain;
 	charset="UTF-8"
-Content-Transfer-Encoding: 8bit
+Content-Transfer-Encoding: 7bit
 
 This is the plain body
 ------_=_NextPart_002_1718977850
 Content-Type: text/html;
 	charset="UTF-8"
-Content-Transfer-Encoding: 8bit
+Content-Transfer-Encoding: 7bit
 
 <body>This is the HTML body</body>
 ------_=_NextPart_002_1718977850--"#,
@@ -875,12 +892,12 @@ Content-Transfer-Encoding: 8bit
             "Sender Näme",
             "from@example.com",
             "Subject Linë",
-            "Lorem Ipsum Dolor Sit\nAmet",
+            "Lorem Ipsum Dolor Sit\nAmët",
         )
         .with_recipient_and_name("Receiver Näme", "receiver@example.com")
         .with_attachment("deadbeef.bin", "application/octet-stream", &bin)
         .with_attachment("🐄💀.bin", "image/bmp", &bin)
-        .with_html_alt("<html lang=\"de-at\"><head></head><body>\n\t<pre>\n\t\tLorem Ipsum Dolor Sit Amet\n\t</pre>\n</body></html>");
+        .with_html_alt("<html lang=\"de-at\"><head></head><body>\n\t<pre>\n\t\tLorem Ipsum Dölor Sit Amet\n\t</pre>\n</body></html>");
 
         let body = mail.format_mail(1732806251).expect("could not format mail");
 
@@ -906,20 +923,16 @@ MIME-Version: 1.0
 ------_=_NextPart_002_1732806251
 Content-Type: text/plain;
 	charset="UTF-8"
-Content-Transfer-Encoding: 8bit
+Content-Transfer-Encoding: base64
 
-Lorem Ipsum Dolor Sit
-Amet
+TG9yZW0gSXBzdW0gRG9sb3IgU2l0CkFtw6t0
 ------_=_NextPart_002_1732806251
 Content-Type: text/html;
 	charset="UTF-8"
-Content-Transfer-Encoding: 8bit
+Content-Transfer-Encoding: base64
 
-<html lang="de-at"><head></head><body>
-	<pre>
-		Lorem Ipsum Dolor Sit Amet
-	</pre>
-</body></html>
+PGh0bWwgbGFuZz0iZGUtYXQiPjxoZWFkPjwvaGVhZD48Ym9keT4KCTxwcmU+CgkJTG9yZW0g
+SXBzdW0gRMO2bG9yIFNpdCBBbWV0Cgk8L3ByZT4KPC9ib2R5PjwvaHRtbD4=
 ------_=_NextPart_002_1732806251--
 ------_=_NextPart_001_1732806251
 Content-Type: application/octet-stream; name="deadbeef.bin"
-- 
2.47.2



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

  parent reply	other threads:[~2025-09-02 14:27 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-02 14:27 [pbs-devel] [PATCH proxmox 0/3] improve mail compatability Shannon Sterz
2025-09-02 14:27 ` [pbs-devel] [PATCH proxmox 1/3] sendmail: encode non-ascii filenames to improve compatability Shannon Sterz
2025-09-02 14:27 ` Shannon Sterz [this message]
2025-09-02 14:27 ` [pbs-devel] [PATCH proxmox 3/3] sendmail: break content disposition headers for attachments Shannon Sterz

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=20250902142720.371671-3-s.sterz@proxmox.com \
    --to=s.sterz@proxmox.com \
    --cc=pbs-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal