8 def subject(to_charset
= 'utf-8')
9 Unquoter
.unquote_and_convert_to(quoted_subject
, to_charset
)
12 def unquoted_body(to_charset
= 'utf-8')
13 from_charset
= sub_header("content-type", "charset")
14 case (content_transfer_encoding
|| "7bit").downcase
15 when "quoted-printable"
16 # the default charset is set to iso-8859-1 instead of 'us-ascii'.
17 # This is needed as many mailer do not set the charset but send in ISO. This is only used if no charset is set.
18 if !from_charset
.blank
? && from_charset
.downcase
== 'us-ascii'
19 from_charset
= 'iso-8859-1'
22 Unquoter
.unquote_quoted_printable_and_convert_to(quoted_body
,
23 to_charset
, from_charset
, true)
25 Unquoter
.unquote_base64_and_convert_to(quoted_body
, to_charset
,
28 Unquoter
.convert_to(quoted_body
, to_charset
, from_charset
)
36 def body(to_charset
= 'utf-8', &block
)
37 attachment_presenter
= block
|| Proc
.new
{ |file_name
| "Attachment: #{file_name}\n" }
40 parts
.collect
{ |part
|
41 header
= part
["content-type"]
44 part
.body(to_charset
, &attachment_presenter
)
47 elsif !attachment
?(part
)
48 part
.unquoted_body(to_charset
)
50 attachment_presenter
.call(header
["name"] || "(unnamed)")
54 unquoted_body(to_charset
)
61 def unquote_and_convert_to(text
, to_charset
, from_charset
= "iso-8859-1", preserve_underscores
=false)
62 return "" if text
.nil?
63 text
.gsub(/(.*?)(?:(?:=\?(.*?)\?(.)\?(.*?)\?=)|$)/) do
69 before
= convert_to(before
, to_charset
, from_charset
) if before
.length
> 0
70 before
+ case quoting_method
72 unquote_quoted_printable_and_convert_to(text
, to_charset
, from_charset
, preserve_underscores
)
74 unquote_base64_and_convert_to(text
, to_charset
, from_charset
)
76 # will be nil at the end of the string, due to the nature of
80 raise "unknown quoting method #{quoting_method.inspect}"
85 def unquote_quoted_printable_and_convert_to(text
, to
, from
, preserve_underscores
=false)
86 text
= text
.gsub(/_/, " ") unless preserve_underscores
87 text
= text
.gsub(/\r\n|\r/, "\n") # normalize newlines
88 convert_to(text
.unpack("M*").first
, to
, from
)
91 def unquote_base64_and_convert_to(text
, to
, from
)
92 convert_to(Base64
.decode(text
), to
, from
)
97 def convert_to(text
, to
, from
)
98 return text
unless to
&& from
99 text
? Iconv
.iconv(to
, from
, text
).first
: ""
100 rescue Iconv
::IllegalSequence, Iconv
::InvalidEncoding, Errno
::EINVAL
101 # the 'from' parameter specifies a charset other than what the text
102 # actually is...not much we can do in this case but just return the
105 # Ditto if either parameter represents an unknown charset, like
110 # Not providing quoting support
111 def convert_to(text
, to
, from
)
112 warn
"Action Mailer: iconv not loaded; ignoring conversion from #{from} to #{to} (#{__FILE__}:#{__LINE__})"