diff options
-rw-r--r-- | lib/mail_handler/backends/mail_extensions.rb | 15 | ||||
-rw-r--r-- | spec/fixtures/files/unrecognized-encoding-mail.email | 36 | ||||
-rw-r--r-- | spec/lib/mail_handler/mail_handler_spec.rb | 5 |
3 files changed, 56 insertions, 0 deletions
diff --git a/lib/mail_handler/backends/mail_extensions.rb b/lib/mail_handler/backends/mail_extensions.rb index 6d6a20f8c..a5b6bc97e 100644 --- a/lib/mail_handler/backends/mail_extensions.rb +++ b/lib/mail_handler/backends/mail_extensions.rb @@ -117,6 +117,21 @@ module Mail end class Ruby19 + def Ruby19.b_value_decode(str) + match = str.match(/\=\?(.+)?\?[Bb]\?(.+)?\?\=/m) + if match + encoding = match[1] + str = Ruby19.decode_base64(match[2]) + # Rescue an ArgumentError arising from an unknown encoding. + begin + str.force_encoding(fix_encoding(encoding)) + rescue ArgumentError + end + end + decoded = str.encode("utf-8", :invalid => :replace, :replace => "") + decoded.valid_encoding? ? decoded : decoded.encode("utf-16le", :invalid => :replace, :replace => "").encode("utf-8") + end + def Ruby19.q_value_decode(str) match = str.match(/\=\?(.+)?\?[Qq]\?(.+)?\?\=/m) if match diff --git a/spec/fixtures/files/unrecognized-encoding-mail.email b/spec/fixtures/files/unrecognized-encoding-mail.email new file mode 100644 index 000000000..266a90fbc --- /dev/null +++ b/spec/fixtures/files/unrecognized-encoding-mail.email @@ -0,0 +1,36 @@ +From xxx@example.com Fri Jun 21 07:50:52 2013 +Return-path: <xxx@example.com> +Envelope-to: xxx@example.com +Delivery-date: Fri, 21 Jun 2013 07:50:52 +0100 +Message-ID: <185C0D48380D7AE612DD38A527D5EAF2@tmvbalem> +From: "cttlqvx" <xxx@example.com> +To: <xxx@example.com> +Subject: =?hz-gb-2312?B?fntPck9ISXpWQn59c3J5dW95d3MoQUQpICAgIA==?= +Date: Fri, 21 Jun 2013 14:48:20 +0800 +MIME-Version: 1.0 +Content-Type: multipart/related; + type="multipart/alternative"; + boundary="----=_NextPart_000_02ED_01A0462A.178683F0" +X-Priority: 1 +X-MSMail-Priority: High +X-Mailer: Microsoft Outlook Express 6.00.2900.5512 +X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5512 + +This is a multi-part message in MIME format. + +------=_NextPart_000_02ED_01A0462A.178683F0 +Content-Type: multipart/alternative; + boundary="----=_NextPart_001_09EC_01A0462A.178683F0" + +------=_NextPart_001_09EC_01A0462A.178683F0 +Content-Type: text/plain; + charset="hz-gb-2312" +Content-Transfer-Encoding: base64 + + +------=_NextPart_001_09EC_01A0462A.178683F0 +Content-Type: text/html; + charset="hz-gb-2312" +Content-Transfer-Encoding: base64 + +------=_NextPart_001_09EC_01A0462A.178683F0-- diff --git a/spec/lib/mail_handler/mail_handler_spec.rb b/spec/lib/mail_handler/mail_handler_spec.rb index d6e7ba5d2..241ce06c1 100644 --- a/spec/lib/mail_handler/mail_handler_spec.rb +++ b/spec/lib/mail_handler/mail_handler_spec.rb @@ -60,6 +60,11 @@ describe 'when creating a mail object from raw data' do body.should match(/ \xe2\x80\x93 /) end + it 'should not error on a subject line with an encoding encoding not recognized by iconv' do + mail = get_fixture_mail('unrecognized-encoding-mail.email') + lambda{ mail.subject }.should_not raise_error + end + end describe 'when asked for the from name' do |