dcparker / ruby-gmail

A Rubyesque interface to Gmail. Connect to Gmail via IMAP and manipulate emails and labels. Send email with your Gmail account via SMTP. Includes full support for parsing and generating MIME messages.
http://dcparker.github.com/ruby-gmail
792 stars 123 forks source link

Support for charset #81

Open ehlyzov opened 9 years ago

ehlyzov commented 9 years ago

If I use search options which are not 'ascii-8bit' encoded, server response is:

[1] pry(main)> mailbox.inbox.emails(gm: 'Привет из Питера!')
Encoding::CompatibilityError: incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string)

String#force_encoding can be used, but in this case Gmail must be aware of original encoding. Otherwise it response with Net::IMAP::BadResponseError: Could not parse command

So I patched Mailbox#emails (https://github.com/dcparker/ruby-gmail/compare/master...ehlyzov:master#diff-41778eca41b3a50379aa723a35b177db) but I don't sure if opts is a good place for charset configuration. Maybe it is better to create the mailbox attribute. What do you think?

Anyway, with this change following code looks clumsy, but it works:

mailbox.inbox.emails(gm: 'Привет из Питера!'.force_encoding('ascii-8bit'), charset: 'UTF-8')

Probably it would be better to make this default behaviour, can't imagine any drawbacks.