sup

A curses threads-with-tags style email client

sup.git

git clone https://supmua.dev/git/sup/
commit 154ff507798d88288e3b265049244322833a40bc
parent eae4a257f16aea67d34957b9a0f64689683a5f90
Author: Dan Callaghan <djc@djc.id.au>
Date:   Thu, 19 Nov 2026 19:36:40 +1100

fix wrong top-level Content-Type when sending messages with attachments

Commit 38f50a6ce0 regressed the Content-Type headers for a message with
an attachment. The Content-Type header needs to be filled before the
body gets converted in-place into an inline part, so that it goes along
with the part and the top level becomes multipart/mixed.

Fixes #629.

Diffstat:
M lib/sup/modes/edit_message_mode.rb | 4 ++--
M test/unit/test_edit_message_mode.rb | 28 ++++++++++++++++++++++++++++
2 files changed, 30 insertions(+), 2 deletions(-)
diff --git a/lib/sup/modes/edit_message_mode.rb b/lib/sup/modes/edit_message_mode.rb
@@ -601,6 +601,8 @@ protected
 
   def build_message date
     m = RMail::Message.new
+    m.header["Content-Type"] = "text/plain; charset=#{$encoding}"
+    m.header["Content-Transfer-Encoding"] ||= +"8bit"
     m.body = @body.join("\n")
     m.body += "\n" + sig_lines.join("\n") unless @sig_edited
     ## body must end in a newline or GPG signatures will be WRONG!
@@ -645,8 +647,6 @@ protected
     m.header["Date"] = date.rfc2822
     m.header["Message-Id"] = @message_id
     m.header["User-Agent"] = "Sup/#{Redwood::VERSION}"
-    m.header["Content-Type"] = "text/plain; charset=#{$encoding}"
-    m.header["Content-Transfer-Encoding"] ||= +"8bit"
     m.header["MIME-Version"] = +"1.0"
     m
   end
diff --git a/test/unit/test_edit_message_mode.rb b/test/unit/test_edit_message_mode.rb
@@ -41,6 +41,27 @@ class TestEditMessageMode < Minitest::Test
     $config = nil
   end
 
+  def test_basic_message
+    opts = {
+      :header => {
+        "From" => +"sender@example.invalid",
+        "To" => +"recip@example.invalid",
+      },
+      :body => ["hello friends"],
+    }
+    mode = Redwood::EditMessageMode.new opts
+
+    msg = mode.send :build_message, Time.now
+    refute msg.multipart?
+    assert_equal "hello friends\n", msg.body
+    assert_equal "text/plain; charset=UTF-8", msg.header["Content-Type"]
+    assert_equal "8bit", msg.header["Content-Transfer-Encoding"]
+    assert_equal "1.0", msg.header["MIME-Version"]
+    assert_match(/^Sup\//, msg.header["User-Agent"])
+    refute_nil msg.header["Date"]
+    refute_nil msg.header["Message-Id"]
+  end
+
   def test_attachment_content_transfer_encoding
     ## RMail::Message#make_attachment will choose
     ## Content-Transfer-Encoding: 8bit for a CSV file.
@@ -120,8 +141,15 @@ class TestEditMessageMode < Minitest::Test
     mode = Redwood::EditMessageMode.new opts
 
     msg = mode.send :build_message, Time.now
+    inline = msg.part(0)
+    assert_equal body_content.b + "\n", inline.body
+    assert_equal "text/plain; charset=UTF-8", inline.header["Content-Type"]
+    assert_equal "inline", inline.header["Content-Disposition"]
+    assert_equal "8bit", inline.header["Content-Transfer-Encoding"]
     attachment = msg.part(1)
     assert_equal attachment_content.b, attachment.body
+    assert_equal 'text/csv; name="cp1251.csv"', attachment.header["Content-Type"]
+    assert_equal 'attachment; filename="cp1251.csv"', attachment.header["Content-Disposition"]
     assert_equal "8bit", attachment.header["Content-Transfer-Encoding"]
 
     ## Also check the serialized message, which is what the send_message method uses.