Skip to content

Commit

Permalink
Differentiate between HTML and plain text e-mails. Use "<br />" inste…
Browse files Browse the repository at this point in the history
…ad of "\n" for line breaks in HTML e-mails. Added 3 new tests to verify this behaviour.
  • Loading branch information
morgancurrie committed Sep 15, 2010
1 parent 49862fd commit 5950209
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/mail_magnet/tmail_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def override_recipients!(recipients)
end
parts << "---------------------------"

self.body = parts.join("\n") + "\n\n" + self.body
line_break = self.content_type =~ /(html)/ ? "<br />" : "\n"
self.body = parts.join(line_break) + line_break*2 + self.body
end

def override(method, recipients)
Expand Down
8 changes: 8 additions & 0 deletions spec/app_root/app/models/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@ def letter
subject 'Hello Universe!'
end

def html_letter
recipients 'original.to@example.com'
cc 'original.cc@example.com'
bcc 'original.bcc@example.com'
subject 'Hello Universe!'
content_type "text/html"
end

end
1 change: 1 addition & 0 deletions spec/app_root/app/views/mailer/html_letter.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
HTMLello!
20 changes: 20 additions & 0 deletions spec/mail_magnet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,24 @@
ActionMailer::Base.deliveries.last.subject.should == 'Hello Universe!'
end

it 'should detect the content_type correctly' do
ActionMailer::Base.override_recipients = 'overridden.to@example.com'
Mailer.deliver_letter
ActionMailer::Base.deliveries.last.content_type.should == "text/plain"
Mailer.deliver_html_letter
ActionMailer::Base.deliveries.last.content_type.should include("text/html") # content_type sometimes ends with "; charset=utf-8"
end

it 'should use "\n" for line breaks in plain text emails' do
ActionMailer::Base.override_recipients = 'overridden.to@example.com'
Mailer.deliver_letter
ActionMailer::Base.deliveries.last.body.should include("To: original.to@example.com\n")
end

it 'should use "<br />" for line breaks in HTML emails' do
ActionMailer::Base.override_recipients = 'overridden.to@example.com'
Mailer.deliver_html_letter
ActionMailer::Base.deliveries.last.body.should include('To: original.to@example.com<br />')
end

end

0 comments on commit 5950209

Please sign in to comment.