When using Ruby + IMAP and trying to search a subject with special chars:
imap.uid_search(['SUBJECT', subject, 'NOT', 'SEEN'])
where subject is "Olá", it will fail with:
Encoding::CompatibilityError: incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string)
from /Users/fernando/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/net/imap.rb:1266:in `==='
Specifying the second parameter of uid_search, which is the charset, also doesn't work.
Subjects without special characters works fine. Is there a way to make this work?
This replicates the problem (with the same regexp that net/imap
uses):
# encoding: ascii-8bit
a = /[\x80-\xff\r\n]/n
a =~ "olá".force_encoding('utf-8') # incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string) (Encoding::CompatibilityError)
Two possibilities:
# encoding: ascii-8bit
to the top of your scriptForce the string's encoding over to ascii-8bit
:
imap.uid_search(['SUBJECT', subject.force_encoding('ascii-8bit'), 'NOT', 'SEEN'])