Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) 
imap.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?

like image 657
Oksana Avatar asked Dec 22 '22 22:12

Oksana


1 Answers

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
like image 134
chrpes Avatar answered Jan 05 '23 18:01

chrpes