Skip to content

Commit d8c8db8

Browse files
committed
Add support for passing empty hashes as attachment
1 parent f2bd337 commit d8c8db8

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

lib/pony.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -233,15 +233,17 @@ def build_mail(options)
233233
# we need to explicitly define a second multipart/alternative
234234
# boundary to encapsulate the body-parts within the
235235
# multipart/mixed boundary that will be created automatically.
236-
if options[:attachments] && options[:html_body] && options[:body]
236+
options[:attachments] ||= {}
237+
238+
if options[:attachments].any? && options[:html_body] && options[:body]
237239
mail.part(:content_type => 'multipart/alternative') do |p|
238240
p.html_part = build_html_part(options)
239241
p.text_part = build_text_part(options)
240242
end
241243

242244
# Otherwise if there is more than one part we still need to
243245
# ensure that they are all declared to be separate.
244-
elsif options[:html_body] || options[:attachments]
246+
elsif options[:html_body] || options[:attachments].any?
245247
mail.html_part = build_html_part(options) if options[:html_body]
246248
mail.text_part = build_text_part(options) if options[:body]
247249

@@ -254,7 +256,7 @@ def build_mail(options)
254256
mail[key] = value
255257
end
256258

257-
add_attachments(mail, options[:attachments]) if options[:attachments]
259+
add_attachments(mail, options[:attachments]) if options[:attachments].any?
258260

259261
mail.charset = options[:charset] if options[:charset] # charset must be set after setting content_type
260262

spec/pony_spec.rb

+30
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,24 @@
301301
end
302302

303303
describe "content type" do
304+
shared_examples 'a mail with only html_body and body set' do
305+
it { expect(mail.parts.length).to eq 2 }
306+
it { expect(mail.content_type.to_s).to include( 'multipart/alternative' ) }
307+
it { expect(mail.parts[0].to_s).to include( 'Content-Type: text/html' ) }
308+
it { expect(mail.parts[1].to_s).to include( 'Content-Type: text/plain' ) }
309+
end
310+
311+
context "mail html_body and body" do
312+
subject(:mail) do
313+
Pony.send(:build_mail,
314+
:body => 'test',
315+
:html_body => 'What do you know, Joe?',
316+
)
317+
end
318+
319+
it_behaves_like 'a mail with only html_body and body set'
320+
end
321+
304322
context "mail with attachments, html_body and body " do
305323
subject(:mail) do
306324
Pony.send(:build_mail,
@@ -318,6 +336,18 @@
318336
it { expect(mail.parts[0].parts[1].to_s).to include( 'Content-Type: text/plain' ) }
319337
it { expect(mail.parts[1].to_s).to include( 'Content-Type: text/plain' ) }
320338
end
339+
340+
context "mail html_body and body and empty attachment list" do
341+
subject(:mail) do
342+
Pony.send(:build_mail,
343+
:body => 'test',
344+
:html_body => 'What do you know, Joe?',
345+
:attachments => {},
346+
)
347+
end
348+
349+
it_behaves_like 'a mail with only html_body and body set'
350+
end
321351
end
322352

323353
describe "additional headers" do

0 commit comments

Comments
 (0)