diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 4727518dd..4eae71cdf 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -11,6 +11,7 @@ Features: * Updated README to improve around sending multipart mail @kapfenho * Add delivery_interceptors method to Mail class to fetch registered interceptors @ghousemohamed +* `Mail::ContentTypeField` now supports setting a `boundary_generator`, allowing custom boundary creation behavior @dugancathal Code Improvements: @@ -19,6 +20,7 @@ Code Improvements: * Configure RSpec's zero-monkey patching mode @olleolleolle * Added support for JRuby 9.4 @mikel * Prefer `__dir__` @olleolleolle +* Remove `=` from default mime boundary in `Mail::ContentTypeField` @dugancathal Bug Fixes: diff --git a/lib/mail/fields/content_type_field.rb b/lib/mail/fields/content_type_field.rb index 686d533ae..8ccda08a5 100644 --- a/lib/mail/fields/content_type_field.rb +++ b/lib/mail/fields/content_type_field.rb @@ -14,10 +14,13 @@ def with_boundary(type) new "#{type}; boundary=#{generate_boundary}" end + attr_accessor :boundary_generator + def generate_boundary - "--==_mimepart_#{Mail.random_tag}" + "--#{boundary_generator.call}" end end + self.boundary_generator = -> { "_mimepart_#{Mail.random_tag}" } def initialize(value = nil, charset = nil) if value.is_a? Array diff --git a/spec/mail/fields/content_type_field_spec.rb b/spec/mail/fields/content_type_field_spec.rb index 5753faf17..87175886a 100644 --- a/spec/mail/fields/content_type_field_spec.rb +++ b/spec/mail/fields/content_type_field_spec.rb @@ -180,14 +180,21 @@ end describe "class methods" do + def with_generator(generator) + old, Mail::ContentTypeField.boundary_generator = Mail::ContentTypeField.boundary_generator, generator + yield + ensure + Mail::ContentTypeField.boundary_generator = old + end + it "should give back an initialized instance with a unique boundary" do boundary = Mail::ContentTypeField.with_boundary('multipart/mixed') - expect(boundary.encoded).to match(%r{Content-Type: multipart/mixed;\r\n\sboundary="--==_mimepart_[\w]+_[\w]+"\r\n}) + expect(boundary.encoded).to match(%r{Content-Type: multipart/mixed;\r\n\sboundary=--_mimepart_[\w]+_[\w]+\r\n}) end it "should give back an initialized instance with different type with a unique boundary" do boundary = Mail::ContentTypeField.with_boundary('multipart/alternative') - expect(boundary.encoded).to match(%r{Content-Type: multipart/alternative;\r\n\sboundary="--==_mimepart_[\w]+_[\w]+"\r\n}) + expect(boundary.encoded).to match(%r{Content-Type: multipart/alternative;\r\n\sboundary=--_mimepart_[\w]+_[\w]+\r\n}) end it "should give unique boundaries" do @@ -198,6 +205,12 @@ end end + it "should allow configuring a boundary generator" do + with_generator(-> { 'arbitrary-boundary' }) do + boundary = Mail::ContentTypeField.with_boundary('multipart/alternative') + expect(boundary.encoded).to match(%r{Content-Type: multipart/alternative;\r\n\sboundary=--arbitrary-boundary}) + end + end end describe "Testing a bunch of email Content-Type fields" do diff --git a/spec/mail/mime_messages_spec.rb b/spec/mail/mime_messages_spec.rb index 024b68684..c6d41b708 100644 --- a/spec/mail/mime_messages_spec.rb +++ b/spec/mail/mime_messages_spec.rb @@ -282,14 +282,14 @@ content_type "text/html; charset=US-ASCII" body "This is HTML" end - expect(mail.to_s).to match(%r|Content-Type: multipart/alternative;\s+boundary="#{mail.boundary}"|) + expect(mail.to_s).to match(%r|Content-Type: multipart/alternative;\s+boundary=#{mail.boundary}|) end it "should set the content type to multipart/alternative if you declare html and text parts" do mail = Mail.new mail.text_part { } mail.html_part { } - expect(mail.to_s).to match(%r|Content-Type: multipart/alternative;\s+boundary="#{mail.boundary}"|) + expect(mail.to_s).to match(%r|Content-Type: multipart/alternative;\s+boundary=#{mail.boundary}|) end it "should not set the content type to multipart/alternative if you declare an html part but not a text part" do