Comments (17)
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.
Mail 2.2.0 should resolve this, please let me know.
from mail.
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.
Same error on mail 2.2.6
from mail.
Same with 2.2.7
from mail.
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.
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.
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.
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.
@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.
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.
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.
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.
Some changes made to the previous patch and submitted. See #174 for more details
edit: changed pull link
from mail.
+1 for Ils. Using it on 1.9.2 and it finally solves the problem. Thanks man!
from mail.
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.
Gem 2.2.14 includes these fixes, thanks Luis!!
from mail.
Related Issues (20)
- Address list with comment before address parses incorrectly if no space after comma
- Gmail SMTP relay issues after upgrading to 2.8.1 HOT 2
- Forward email doesn't encode the attachment correctly HOT 1
- Running into an issue while sending an SMTP mail with v2.7.1 on Ruby 3.0.0 on container
- Encoding
- Parsing a `Mail::Message` from a string ignores the string's encoding
- Net::IMAP connection uses insecure deprecated options HOT 7
- Delivery with RCPT / NOTIFY not achievable
- Where to Report Vulnerabilities? HOT 2
- Email content tampering vulnerability due to crafted file names
- Mail::AddressList cannot parse CC header value
- [Question] Sending email directly to the recipient without a MTA (sendmail, postfix) and without third-party services
- Uniform interface for getting email body? HOT 1
- Excessive memory usage with attachments
- read_only: true sets email to read
- Release a new version?
- Parse attachment filenames when missing RFC-required quotes, for certain content-types?
- FWS handling in message ids
- [Question] PR for AWS SES Support?
- Mail.find(keys: "ALL", order: :desc, count: 20, what: :first) doesn't return the most 20 recent messages. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mail.