Skip to content

Fix that Message#attachments cannot count singlepart attachment #1144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Bugs:
* #1110 - Fix that Mail::Multibyte::Chars#initialize mutated its argument by calling force_encoding on it. (jeremy)
* #1113 - Eliminate attachment corruption caused by CRLF conversion. (jeremy)
* #1131 - Fix that Message#without_attachments! didn't parse the remaining parts. (jeremy)
* #1143 - Fix that Message#attachments cannot count singlepart attachment. (kirikak2)

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

Expand Down
8 changes: 5 additions & 3 deletions lib/mail/attachments_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
module Mail
class AttachmentsList < Array

def initialize(parts_list)
def initialize(parts_list, body_attachment = nil)
@parts_list = parts_list
@content_disposition_type = 'attachment'
parts_list.map { |p|
if p.mime_type == 'message/rfc822'
[body_attachment].concat(parts_list).map { |p|
if p.nil?
nil
elsif p.mime_type == 'message/rfc822'
Mail.new(p.body.encoded).attachments
elsif p.parts.empty?
p if p.attachment?
Expand Down
20 changes: 19 additions & 1 deletion lib/mail/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1655,7 +1655,11 @@ def parts
# mail.attachments[0] #=> Mail::Part (first attachment)
#
def attachments
parts.attachments
if !multipart? && attachment? && !self.instance_of?(Mail::Part)
parts.attachments(generate_attachment_part_from_body)
else
parts.attachments
end
end

def has_attachments?
Expand Down Expand Up @@ -2167,5 +2171,19 @@ def do_delivery
def decode_body_as_text
Encodings.transcode_charset decode_body, charset, 'UTF-8'
end

def generate_attachment_part_from_body
part = Mail::Part.new(nil)
header_part = []
header_part << header[:content_type].encoded if header[:content_type]
header_part << header[:content_disposition].encoded if header[:content_disposition]
header_part << header[:content_transfer_encoding].encoded if header[:content_transfer_encoding]
header_part << header[:content_discription].encoded if header[:content_discription]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

header[:content_description] typo here

part.header = header_part.join
unless @body.nil?
part.body = @body.encoded
end
part
end
end
end
4 changes: 2 additions & 2 deletions lib/mail/parts_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def to_yaml(options = {}) # :nodoc:
@parts.to_yaml(options)
end

def attachments
Mail::AttachmentsList.new(@parts)
def attachments(body_attachment = nil)
Mail::AttachmentsList.new(@parts, body_attachment)
end

def collect
Expand Down
10 changes: 10 additions & 0 deletions spec/mail/attachments_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ def check_decoded(actual, expected)

end

describe "single attachment" do
subject{ read_fixture("emails", "attachment_emails", "attachment_only_email.eml") }

it "should get one attachment from #attachments method" do
expect(subject.attachments.length).to eq(1)
expect(subject.attachments[0].filename).to eq("blah.gz")
expect(subject.attachments[0].decoded).not_to be_nil
end
end

describe "multiple attachments" do

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