Skip to content


Subversion checkout URL

You can clone with
Download ZIP


can't create emails with body in ISO-2022-JP encoding #342

mreinsch opened this Issue · 3 comments

3 participants


Assume, for instance, the following code to create an ISO-2022-JP email (commonly used by Japanese feature phones for email):

mail = { content_type ["text", "plain", { "charset" => "ISO-2022-JP" }] }
mail.body = "にほんごです".encode("ISO-2022-JP")
puts mail.to_s

This produces the following exception:

   incompatible encoding regexp match (US-ASCII regexp with ISO-2022-JP string)
 # mail/lib/mail/core_extensions/string.rb:13:in `=~'
 # mail/lib/mail/core_extensions/string.rb:13:in `!~'
 # mail/lib/mail/core_extensions/string.rb:13:in `blank?'
 # mail/lib/mail/body.rb:36:in `initialize'
 # mail/lib/mail/message.rb:1909:in `new'
 # mail/lib/mail/message.rb:1909:in `process_body_raw'
 # mail/lib/mail/message.rb:1155:in `body'
 # mail/lib/mail/message.rb:1934:in `identify_and_set_transfer_encoding'
 # mail/lib/mail/message.rb:1689:in `ready_to_send!'
 # mail/lib/mail/message.rb:1707:in `encoded'
 # mail/lib/mail/message.rb:1776:in `to_s'
knu commented

The library should be able to handle non-ASCII compatible strings, and ISO-2022-JP support is indeed very important in Japanese market and community.

For ISO-2022-JP(_KDDI) input, you can temporarily encode it into Encoding::Stateless_ISO_2022_JP(_KDDI), do regexp processing, and then encode back to ISO-2022-JP.

For other non-ASCII compatible strings, you can use force_encoding(Encoding::ASCII_8BIT) to enable regexp processing and force_encoding(original_encoding) to set it back, so long as what you do with regexps do not include up/downcasing, word matching or folding but just binary processing or whitespace processing that rarely do harm in any encoding.


Thanks for your comments (also on twitter :) - I'll try that.
I had already suggested to force_encoding("binary") any incoming String in #341 to fix #340 - but someone correctly observed that this would break 1.8 compatibility - well, time to move on and deprecate 1.8, imho.


See for a fix. Please do investigate merging this back to the mainline mail lib :bowtie:

@jeremy jeremy closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.