Skip to content

Commit

Permalink
encode attachments if they have lines longer than 998 chars
Browse files Browse the repository at this point in the history
  • Loading branch information
danc86 committed Jul 7, 2024
1 parent f3b6312 commit ac12be5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions Manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ test/unit/test_edit_message_mode.rb
test/unit/test_horizontal_selector.rb
test/unit/test_locale_fiddler.rb
test/unit/test_person.rb
test/unit/test_rmail_message.rb
test/unit/util/test_query.rb
test/unit/util/test_string.rb
test/unit/util/test_uri.rb
9 changes: 8 additions & 1 deletion lib/sup/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,14 @@ class Message
def self.make_file_attachment fn
bfn = File.basename fn
t = MIME::Types.type_for(bfn).first || MIME::Types.type_for("exe").first
make_attachment IO.read(fn), t.content_type, t.encoding, bfn.to_s
payload = IO.read fn
## Need to encode as base64 or quoted-printable if any lines are longer than 998 chars.
encoding = if t.encoding != t.default_encoding and payload.each_line.any? { |l| l.length > 998 }
t.default_encoding
else
t.encoding
end
make_attachment payload, t.content_type, encoding, bfn.to_s
end

def charset
Expand Down
36 changes: 36 additions & 0 deletions test/unit/test_rmail_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "test_helper"

require "sup"

class TestRMailMessage < Minitest::Test
def setup
@path = Dir.mktmpdir
end

def teardown
FileUtils.rm_r @path
end

def test_make_file_attachment
filename = File.join @path, "test.html"
File.write filename, "<html></html>"

a = RMail::Message.make_file_attachment(filename)
assert_equal "text/html; name=\"test.html\"", a.header["Content-Type"]
assert_equal "attachment; filename=\"test.html\"", a.header["Content-Disposition"]
assert_equal "8bit", a.header["Content-Transfer-Encoding"]
end

def test_make_file_attachment_text_with_long_lines
filename = File.join @path, "test.html"
File.write filename, "a" * 1023

a = RMail::Message.make_file_attachment(filename)
assert_equal "text/html; name=\"test.html\"", a.header["Content-Type"]
assert_equal "attachment; filename=\"test.html\"", a.header["Content-Disposition"]
assert_equal "quoted-printable", a.header["Content-Transfer-Encoding"]

qp_encoded = ("a" * 73 + "=\n") * 14 + "a=\n"
assert_equal qp_encoded, a.body
end
end

0 comments on commit ac12be5

Please sign in to comment.