Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configurable mime boundary generator #1481

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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:

Expand Down
5 changes: 4 additions & 1 deletion lib/mail/fields/content_type_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions spec/mail/fields/content_type_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/mail/mime_messages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,14 @@
content_type "text/html; charset=US-ASCII"
body "<b>This is HTML</b>"
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
Expand Down