Skip to content

Commit 8fff96d

Browse files
committed
Fix that Message#attachemnts cannot count singlepart attachment
1 parent f0dbe48 commit 8fff96d

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

CHANGELOG.rdoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Bugs:
5858
* #1110 - Fix that Mail::Multibyte::Chars#initialize mutated its argument by calling force_encoding on it. (jeremy)
5959
* #1113 - Eliminate attachment corruption caused by CRLF conversion. (jeremy)
6060
* #1131 - Fix that Message#without_attachments! didn't parse the remaining parts. (jeremy)
61+
* #1143 - Fix that Message#attachment cannot count singlepart attachment. (kirikak2)
6162

6263
== Version 2.6.4 - Wed Mar 23 08:16 -0700 2016 Jeremy Daer <[email protected]>
6364

lib/mail/attachments_list.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
module Mail
33
class AttachmentsList < Array
44

5-
def initialize(parts_list)
5+
def initialize(body_attachment = nil, parts_list)
66
@parts_list = parts_list
77
@content_disposition_type = 'attachment'
8-
parts_list.map { |p|
9-
if p.mime_type == 'message/rfc822'
8+
[body_attachment].concat(parts_list).map { |p|
9+
if p.nil?
10+
nil
11+
elsif p.mime_type == 'message/rfc822'
1012
Mail.new(p.body.encoded).attachments
1113
elsif p.parts.empty?
1214
p if p.attachment?

lib/mail/message.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1655,7 +1655,11 @@ def parts
16551655
# mail.attachments[0] #=> Mail::Part (first attachment)
16561656
#
16571657
def attachments
1658-
parts.attachments
1658+
if !multipart? && attachment? && !self.instance_of?(Mail::Part)
1659+
parts.attachments(generate_attachment_part_from_body)
1660+
else
1661+
parts.attachments
1662+
end
16591663
end
16601664

16611665
def has_attachments?
@@ -2167,5 +2171,19 @@ def do_delivery
21672171
def decode_body_as_text
21682172
Encodings.transcode_charset decode_body, charset, 'UTF-8'
21692173
end
2174+
2175+
def generate_attachment_part_from_body
2176+
part = Mail::Part.new(nil)
2177+
header_part = []
2178+
header_part << header[:content_type].encoded if header[:content_type]
2179+
header_part << header[:content_disposition].encoded if header[:content_disposition]
2180+
header_part << header[:content_transfer_encoding].encoded if header[:content_transfer_encoding]
2181+
header_part << header[:content_discription].encoded if header[:content_discription]
2182+
part.header = header_part.join
2183+
unless @body.nil?
2184+
part.body = @body.encoded
2185+
end
2186+
part
2187+
end
21702188
end
21712189
end

lib/mail/parts_list.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ def to_yaml(options = {}) # :nodoc:
2121
@parts.to_yaml(options)
2222
end
2323

24-
def attachments
25-
Mail::AttachmentsList.new(@parts)
24+
def attachments(body_attachment = nil)
25+
Mail::AttachmentsList.new(body_attachment, @parts)
2626
end
2727

2828
def collect

spec/mail/attachments_list_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ def check_decoded(actual, expected)
128128

129129
end
130130

131+
describe "single attachment" do
132+
subject{ read_fixture("emails", "attachment_emails", "attachment_only_email.eml") }
133+
134+
it "should get one attachment from #attachments method" do
135+
expect(subject.attachments.length).to eq(1)
136+
expect(subject.attachments[0].filename).to eq("blah.gz")
137+
expect(subject.attachments[0].decoded).not_to be_nil
138+
end
139+
end
140+
131141
describe "multiple attachments" do
132142

133143
it "should allow you to pass in more than one attachment" do

0 commit comments

Comments
 (0)