Skip to content

Instantly share code, notes, and snippets.

@kakutani
Created January 13, 2011 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kakutani/777828 to your computer and use it in GitHub Desktop.
Save kakutani/777828 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
module Iso2022jpEnforcer
extend ActiveSupport::Concern
included do
default :charset => 'iso-2022-jp'
alias_method_chain :mail, :iso_2022_jp
end
module ClassMethods
# taken from http://wiki.fdiary.net/rails/?ActionMailer
def base64(text, charset="iso-2022-jp", convert=true)
if convert
if charset == "iso-2022-jp"
text = NKF.nkf('-j -m0', text)
end
end
text = [text].pack('m').delete("\r\n")
"=?#{charset}?B?#{text}?="
end
end
module InstanceMethods
def mail_with_iso_2022_jp(headers, &block)
mail = mail_without_iso_2022_jp(headers, &block)
mail.subject = base64(mail.subject)
mail.body.charset = 'iso-2022-jp'
mail.body.encoding = '7bit'
mail.body = NKF::nkf('-j', mail.body.to_s)
mail
end
private
def base64(text, charset="iso-2022-jp", convert=true)
self.class.base64(text, charset, convert)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment