Skip to content

Commit e482dce

Browse files
committed
Merge pull request rails#27227 from MQuy/allow-custom-content-type-in-mail-body
Allow to custom content type when setting mailer body
2 parents d06d71b + f091bd6 commit e482dce

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

actionmailer/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
* Mime type: allow to custom content type when setting body in headers
2+
and attachments.
3+
4+
Example:
5+
6+
def test_emails
7+
attachments["invoice.pdf"] = "This is test File content"
8+
mail(body: "Hello there", content_type: "text/html")
9+
end
10+
11+
*Minh Quy*
12+
113
* Exception handling: use `rescue_from` to handle exceptions raised by
214
mailer actions, by message delivery, and by deferred delivery jobs.
315

actionmailer/lib/action_mailer/base.rb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,19 @@ module ActionMailer
208208
# end
209209
# end
210210
#
211+
# You can also send attachments with html template, in this case you need to add body, attachments,
212+
# and custom content type like this:
213+
#
214+
# class NotifierMailer < ApplicationMailer
215+
# def welcome(recipient)
216+
# attachments["free_book.pdf"] = File.read("path/to/file.pdf")
217+
# mail(to: recipient,
218+
# subject: "New account information",
219+
# content_type: "text/html",
220+
# body: "<html><body>Hello there</body></html>")
221+
# end
222+
# end
223+
#
211224
# = Inline Attachments
212225
#
213226
# You can also specify that a file should be displayed inline with other HTML. This is useful
@@ -896,15 +909,19 @@ def collect_responses(headers)
896909
yield(collector)
897910
collector.responses
898911
elsif headers[:body]
899-
[{
900-
body: headers.delete(:body),
901-
content_type: self.class.default[:content_type] || "text/plain"
902-
}]
912+
collect_responses_from_text(headers)
903913
else
904914
collect_responses_from_templates(headers)
905915
end
906916
end
907917

918+
def collect_responses_from_text(headers)
919+
[{
920+
body: headers.delete(:body),
921+
content_type: headers[:content_type] || "text/plain"
922+
}]
923+
end
924+
908925
def collect_responses_from_templates(headers)
909926
templates_path = headers[:template_path] || self.class.mailer_name
910927
templates_name = headers[:template_name] || action_name

actionmailer/test/base_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ class BaseTest < ActiveSupport::TestCase
140140
assert_equal("multipart/mixed", email.mime_type)
141141
end
142142

143+
test "set mime type to text/html when attachment is included and body is set" do
144+
email = BaseMailer.attachment_with_content(body: "Hello there", content_type: "text/html")
145+
assert_equal("text/html", email.mime_type)
146+
end
147+
143148
test "adds the rendered template as part" do
144149
email = BaseMailer.attachment_with_content
145150
assert_equal(2, email.parts.length)

0 commit comments

Comments
 (0)