Skip to content

Commit

Permalink
Reuse body encoding
Browse files Browse the repository at this point in the history
This prevents emails from being garbled when delivered.

Closes #166
  • Loading branch information
fphilipe committed Jul 6, 2016
1 parent 5bb22d7 commit 0ac90cf
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## HEAD

- Improve check for Rails module
- Preserve body encoding to prevent garbled mails

## v1.9.3

Expand Down
26 changes: 18 additions & 8 deletions lib/premailer/rails/hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,27 @@ def generate_html_part
# can end up containing CSS rules.
generate_text_part if generate_text_part?

Mail::Part.new(
content_transfer_encoding: html_part.content_transfer_encoding,
content_type: "text/html; charset=#{html_part.charset}",
body: premailer.to_inline_css)
part = html_part
html = premailer.to_inline_css
Mail::Part.new do
content_transfer_encoding part.content_transfer_encoding
content_type "text/html; charset=#{part.charset}"
body html
body_encoding part.body.encoding
end
end

def generate_text_part
@text_part ||= Mail::Part.new(
content_transfer_encoding: html_part.content_transfer_encoding,
content_type: "text/plain; charset=#{html_part.charset}",
body: premailer.to_plain_text)
@text_part ||= begin
part = html_part
text = premailer.to_plain_text
Mail::Part.new do
content_transfer_encoding part.content_transfer_encoding
content_type "text/plain; charset=#{part.charset}"
body text
body_encoding part.body.encoding
end
end
end

def premailer
Expand Down
16 changes: 16 additions & 0 deletions spec/integration/hook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ def run_hook(message)
Premailer::Rails::Hook.perform(message)
end

def body_content(message)
Nokogiri::HTML(message.html_string).at('body').content
end

class Mail::Message
def html_string
(html_part || self).body.to_s
Expand Down Expand Up @@ -54,6 +58,18 @@ def html_string
end
end

it 'does not screw up the text by maintaining the original body encoding' do
raw_msg = Fixtures::Message.latin_message
processed_msg = Fixtures::Message.latin_message
run_hook(processed_msg)
expect(body_content(processed_msg)).to eq(body_content(raw_msg))

raw_msg = Fixtures::Message.non_latin_message
processed_msg = Fixtures::Message.non_latin_message
run_hook(processed_msg)
expect(body_content(processed_msg)).to eq(body_content(raw_msg))
end

it 'generates a text part from the html' do
expect { run_hook(message) }.to change(message, :text_part)
end
Expand Down
16 changes: 16 additions & 0 deletions spec/support/fixtures/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ def with_body(body_type)
message
end

def latin_message
base_message.tap do |message|
message.body = HTML_PART
message.content_type 'text/html; charset=UTF-8'
message.ready_to_send!
end
end

def non_latin_message
base_message.tap do |message|
message.body = HTML_PART_WITH_UNICODE
message.content_type 'text/html; charset=UTF-8'
message.ready_to_send!
end
end

private

def base_message
Expand Down

0 comments on commit 0ac90cf

Please sign in to comment.