A patch that provides 'mail' gem with iso-2022-jp conversion capability.

Overview

  • (en)

    mail-iso-2022-jp is a patch for mikel/mail. With this patch, you can easily send mails with ISO-2022-JP enconding (so-called "JIS-CODE").

  • (ja)

    mail-iso-2022-jp は、mikel/mail に対するパッチです。 これを利用すると ISO-2022-JP(いわゆる「JISコード」)でのメール送信が容易になります。

Feature

  • (en)

    If you set the charset header to ISO-2022-JP, the values of From, Sender, To, Cc, Reply-To, Subject, Resent-From, Resent-Sender, Resent-To and Resent-Cc headers and the text of body will be automatically converted to ISO-2022-JP.

    When the charset header has other values, this patch has no effect.

  • (ja)

    chasetヘッダの値が ISO-2022-JP である場合、差出人(From)、Sender、宛先(To)、Cc、Reply-To、件名(Subject)、 Resent-From、Resent-Sender、Resent-To、Resent-Cc の各ヘッダの値および本文(Body)が 自動的に ISO-2022-JP に変換されます。

    charsetヘッダの値が ISO-2022-JP でない場合、このパッチには何の効果もありません。

Requirements

Ruby

  • 1.8.7, 1.9.x

Gems

  • mail 2.2.6 or higher, but lower than or equal to 2.5.2

Ruby on Rails (Optional)

  • 3.0 or higher

Getting Start

Install as a gem

Add to your Gemfile:

gem 'mail-iso-2022-jp'

or run this command:

gem install mail-iso-2022-jp

Install as a Rails plugin

$ cd RAILS_ROOT
$ rails plugin install git://github.com/kuroda/mail-iso-2022-jp.git

Usage

mail = Mail.new(:charset => 'ISO-2022-JP') do
  from    '山田太郎 <[email protected]>'
  to      '佐藤花子 <[email protected]>'
  cc      '事務局 <[email protected]>'
  subject '日本語件名'
  body    '日本語本文'
end

mail['from'].encoded
  => "From: =?ISO-2022-JP?B?GyRCOzNFREJATzobKEI=?= <[email protected]>\r\n"
mail['to'].encoded
  => "To: =?ISO-2022-JP?B?GyRCOjRGIzJWO1IbKEI=?= <[email protected]>\r\n"
mail.subject
 => "=?ISO-2022-JP?B?GyRCRnxLXDhsN29MPhsoQg==?="
NKF.nkf('-mw', mail.subject)
 => "日本語件名"
mail.body.encoded
 => "\e$BF|K\\8lK\\J8\e(B"
NKF.nkf('-w', mail.body.encoded)
 => "日本語本文"

Usage with ActionMailer

class UserMailer < ActionMailer::Base
  default  :charset => 'ISO-2022-JP',
    :from => "山田太郎 <[email protected]>",
    :cc => '事務局 <[email protected]>'

  def notice
    mail(:to => '佐藤花子 <[email protected]>', :subject => '日本語件名') do |format|
      format.text { render :inline => '日本語本文' }
    end
  end
end

Remarks

  • などのNEC特殊文字や といったIBM拡張文字を件名、宛先、本文などに含めることができます。
  • 全角チルダ(U+FF5E)は波ダッシュ(U+301C)に変換されます。
  • 半角カタカナはそのまま維持されます。
  • 変換できない文字は疑問符(?)で置換されます。

References

License

(en) mail-iso-2022-jp is distributed under the MIT license. (MIT-LICENSE)

(ja) mail-iso-2022-jp はMITライセンスで配布されています。 (MIT-LICENSE)

Special thanks

Kohei Matsushita -- Initial creator of this patch.