Ruby IMAP library doesn't decode mail subject
I have got mail message with following subject in my Gmail accout:
"400, значение, значение"
Here is the code I use to grab mail:
imap = Net::IMAP.new('imap.gmail.com', 993, true, nil, false)
imap.login(LOGIN, PASSWORD)
i开发者_如何学运维map.select("INBOX")
messages = imap.search(['ALL']).map do |message_id|
msg =imap.fetch(message_id, "ENVELOPE")[0].attr["ENVELOPE"]
result = {:mailbox => msg.from[0].mailbox, :host => msg.from[0].host, :subject => msg.subject, :created_at => msg.date}
imap.store(message_id, "+FLAGS", [:Deleted])
result
end
imap.expunge()
imap.logout
In msg.subject i've got following value "=?KOI8-R?B?MTAwLCDixc7ayc4sIDMwMDAgzMnU0s/X?="
It seems that IMAP haven't decoded it. Should I do I manually or IMAP library could it for me?
Mail::Encodings is really helpful here:
require 'mail'
test = "zwei plus =?ISO-8859-15?Q?zw=F6lf_ist_vierzehn?="
puts Mail::Encodings.value_decode(test)
returns
zwei plus zwölf ist vierzehn
How about using NKF?
require 'nkf'
...
result = {... :subject => NKF.nkf("-mw", msg.subject), ...}
-mw means MIME decode and utf-8 output
精彩评论