Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Encoded attachment filename is not decoded #83

Closed
lls opened this Issue · 7 comments

6 participants

@lls

I have found a it rare mail which has an attach whose filename is encoded ("=?utf-8?b?Rm90bzAwMDkuanBn?="). Filename is not decoded when trying to read it (mail.attachments[0].filename). It's easy to reproduce (below an example). If I apply a "Mail::Encodings.decode_encode" it is decoded fine:

require 'rubygems'
require 'mail'

data = <<-limitMAIL
Subject: aaa
From: aaa@aaa.com
To: bbb@aaa.com
Content-Type: multipart/mixed; boundary=0016e64c0af257c3a7048b69e1ac

--0016e64c0af257c3a7048b69e1ac
Content-Type: multipart/alternative; boundary=0016e64c0af257c3a1048b69e1aa

--0016e64c0af257c3a1048b69e1aa
Content-Type: text/plain; charset=ISO-8859-1

aaa

--0016e64c0af257c3a1048b69e1aa
Content-Type: text/html; charset=ISO-8859-1

aaa<br>

--0016e64c0af257c3a1048b69e1aa--
--0016e64c0af257c3a7048b69e1ac
Content-Type: text/plain; charset=US-ASCII; name="=?utf-8?b?Rm90bzAwMDkuanBn?="
Content-Disposition: attachment; filename="=?utf-8?b?Rm90bzAwMDkuanBn?="
Content-Transfer-Encoding: base64
X-Attachment-Id: f_gbneqxxy0

YWFhCg==
--0016e64c0af257c3a7048b69e1ac--
limitMAIL

mail = Mail.new(data)
puts mail.attachments[0].filename
puts Mail::Encodings.decode_encode(mail.attachments[0].filename, :decode)

it results in:

irb(main):035:0> puts mail.attachments[0].filename
=?utf-8?b?Rm90bzAwMDkuanBn?=
=> nil
irb(main):036:0> puts Mail::Encodings.decode_encode(mail.attachments[0].filename, :decode)
Foto0009.jpg
=> nil
@amishyn

Or next spec will fail

it "should work with utf-8 encoded filenames" do
  file_data = File.read(filename = fixture('attachments', 'test.png'))
  @mail.attachments["тестовое письмо.png"] = File.read(fixture('attachments', 'test.png'))
  @mail.attachments
end
@amishyn

I found the next bug in Mail::ContentDispositionParser

<Mail::Field:0x1036e1348 @field=#<Mail::UnstructuredField:0x1036c4838 @tree=nil, @charset="UTF8", @length=nil, @name="content-disposition", @errors=[["content-disposition", "attachment; filename=\"тестовое письмо.png\"", #<Mail::Field::ParseError: ContentDispositionElement can not parse |attachment; filename="тестовое письмо.png"|
Reason was: Expected one of
, \, (, ; at line 1, column 23 (byte 23) after

]], @element=nil, @value="attachment; filename=\"тестовое письмо.png\"">>

@amatsuda

I just fixed this problem and sent a pull request: #158

Hope you to try it out and +1 for the pull request.

@lls
lls commented

The patch doesn't work for my first sample since you have modified []= for the AttachmentList class but attachments are passed in the initializer (in the parts_list argument) and the []= is never called.

@amatsuda

Em... OK, I was wrong.

Actually my patch fixes amishyn's case (which definitely is a bug) but not lls's original issue.
I'm not sure whether lls's issue should be fixes or not.

@kewinwang

html_part do
content_type 'text/html; charset=US-ASCII'
body "

Hello! friend:


end
then print the encoding problem:
Non US-ASCII detected and no content-transfer-encoding defined.
Defaulting to 8bit, set your own if this is incorrect.

I don't know why ,anybody meet it ??

@lawrencepit

@mikel this can be closed, the test in the first comment results correctly in:

Foto0009.jpg
Foto0009.jpg
@jeremy jeremy closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.