Git Product home page Git Product logo

Comments (17)

lls avatar lls commented on July 20, 2024

Seems it happens with subject too. If I try this:

require 'rubygems'
require 'iconv'
require 'mail'

mail = Mail.new "To:[email protected]\nFrom: [email protected]\nSubject: =?ISO-8859-1?Q?2_=FAlt?=\n\nsome text\n\n"

p mail.subject
puts mail.subject
p Iconv.conv("iso-8859-1", "utf-8", mail.subject)
p mail.subject.unpack("U*")

I get this result:

$ irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'iconv'
=> true
irb(main):003:0> require 'mail'
=> true
irb(main):004:0> 
irb(main):005:0* mail = Mail.new "To:[email protected]\nFrom: [email protected]\nSubject: =?ISO-8859-1?Q?2_=FAlt?=\n\nsome text\n\n"
=> #<Mail::Message:70210791052740, Multipart: false, Headers: <From: [email protected]>, <To: [email protected]>, <Subject: =?ISO-8859-1?Q?2_=FAlt?=>>
irb(main):006:0> 
irb(main):007:0* p mail.subject
"2 \372lt"
=> nil
irb(main):008:0> puts mail.subject
2 ?lt
=> nil
irb(main):009:0> p Iconv.conv("iso-8859-1", "utf-8", mail.subject)
Iconv::IllegalSequence: "\372lt"
    from (irb):9:in `conv'
    from (irb):9
irb(main):010:0> p mail.subject.unpack("U*")
ArgumentError: malformed UTF-8 character (expected 5 bytes, given 3 bytes)
    from (irb):10:in `unpack'
    from (irb):10

So the result string isn't either iso-8859-1 nor utf-8

from mail.

mikel avatar mikel commented on July 20, 2024

Mail 2.2.0 should resolve this, please let me know.

from mail.

lls avatar lls commented on July 20, 2024

Same problem with 2.2.0. Previous sample is not working (same result)

irb(main):011:0> Mail::VERSION::STRING
=> "2.2.0"

from mail.

petRUShka avatar petRUShka commented on July 20, 2024

Same error on mail 2.2.6

from mail.

rfc2822 avatar rfc2822 commented on July 20, 2024

Same with 2.2.7

from mail.

bluecat76 avatar bluecat76 commented on July 20, 2024

I found the reason of this being that the body/chunk text is not saved using the given encoding. I could fix this in my fork, but it didn't pass the specs, because I worked for Ruby 1.9 only.

Maybe someone can check my commit and improve it to work for Ruby 1.8 also. There are only a few changes necessary, see here:
http://github.com/bluecat76/mail/commit/d12e32ab756753e7b3de5acc2f7b033167d1d00f

BTW, the set_charset in Mail::Body is unnecessary, because only_us_ascii is always true since the content is escaped.

from mail.

abhishiv avatar abhishiv commented on July 20, 2024

Did someone fix it for ruby 1.8.6? I would do it myself, but I am not that proficient in ruby yet.

from mail.

rfc2822 avatar rfc2822 commented on July 20, 2024

I think commit ...d1d00f fixes issue 129 for Ruby 1.9 but not this issue 44 because it deals with the body and not header fields. Is this correct?

from mail.

abhishiv avatar abhishiv commented on July 20, 2024

I havent checked it for ruby 1.9, because I am using heroku.

It's such a serious issues, has been hindering me from using mail for months.

from mail.

bluecat76 avatar bluecat76 commented on July 20, 2024

@rfc2822: yes, this fix only covers the body part.

But AFAIK the ".force_encoding" should better call a function in the mail component's version_specific classes. Unfortunately this is over my head ATM. The same applies for a fix to the header fields...

from mail.

rfc2822 avatar rfc2822 commented on July 20, 2024

I think for Ruby 1.8 we would have to deal with iconv... much more complicated because the strings don't deal with charsets themselves...

from mail.

lls avatar lls commented on July 20, 2024

Applying this patch, make my first sample work (ruby 1.9).

module Mail
  class UnstructuredField
    def do_decode
      ret = value.blank? ? nil : Encodings.decode_encode(value, :decode).encode("UTF-8")
    end
  end
end

from mail.

lls avatar lls commented on July 20, 2024

Somewhat related to this problem, trying this (ruby 1.9.2 and mail "2.2.12"):

require 'mail'
Mail.new("To:[email protected]\nFrom: [email protected]\nSubject: ma=?ISO-8859-1?Q?=F1ana?=\n\nsome text\n\n").subject

I get this result
"ma=?ISO-8859-1?Q?=F1ana?="

Applying the previous patch an another one seems to solve the problem:
require 'mail'

module Mail
  class UnstructuredField
    def do_decode
      value.blank? ? nil : Encodings.decode_encode(value, :decode).encode("UTF-8")
    end
  end
  module Encodings
    def Encodings.value_decode(str)
      # Optimization: If there's no encoded-words in the string, just return it
      return str unless str.index("=?")
      str = str.gsub(/\?=(\s*)=\?/, '?==?') # Remove whitespaces between 'encoded-word's
      # Split on white-space boundaries with capture, so we capture the white-space as well
      str.split(/([ \t])/).map do |text|
    if text.index('=?').nil? #!= 0
      text
    else
      # Join QP encoded-words that are adjacent to avoid decoding partial chars
      text.gsub!(/\?\=\=\?.+?\?[Qq]\?/m, '') if text =~ /\?==\?/

      # Separate encoded-words with a space, so we can treat them one by one
      text.gsub!(/\?\=\=\?/, '?= =?')
      text.split(/ /).map do |word|
        word.to_str.
          gsub(/=\?.+\?[Bb]\?.+\?=/m) {|substr| b_value_decode(substr)}.
          gsub(/=\?.+\?[Qq]\?.+\?=/m) {|substr| q_value_decode(substr)}
#
#            case
#            when word.to_str =~ /=\?.+\?[Bb]\?/m
#              b_value_decode(word)
#            when text.to_str =~ /=\?.+\?[Qq]\?/m
#              q_value_decode(word)
#            else
#              word.to_str
#            end
      end
    end
      end.join("")
    end
  end
end

So now, running the previous code I get
"mañana"

from mail.

lls avatar lls commented on July 20, 2024

Some changes made to the previous patch and submitted. See #174 for more details

edit: changed pull link

from mail.

abhishiv avatar abhishiv commented on July 20, 2024

+1 for Ils. Using it on 1.9.2 and it finally solves the problem. Thanks man!

from mail.

mikel avatar mikel commented on July 20, 2024

Looking good on lls's patch for 1.9.2, just have to clear up some stuff for 1.8 and we'll be good to release a new gem with this fix.

from mail.

mikel avatar mikel commented on July 20, 2024

Gem 2.2.14 includes these fixes, thanks Luis!!

from mail.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.