- if ( $enc ne $charset && $body) {
- my @lines = $body->as_lines or return;
-
- # {{{ Convert the body
- eval {
- $RT::Logger->debug("Converting '$charset' to '$enc' for ". $head->mime_type . " - ". ($head->get('subject') || 'Subjectless message'));
-
- # NOTE:: see the comments at the end of the sub.
- Encode::_utf8_off( $lines[$_] ) foreach ( 0 .. $#lines );
- Encode::from_to( $lines[$_], $charset => $enc ) for ( 0 .. $#lines );
- };
-
- if ($@) {
- $RT::Logger->error( "Encoding error: " . $@ . " defaulting to ISO-8859-1 -> UTF-8" );
- eval {
- Encode::from_to( $lines[$_], 'iso-8859-1' => $enc ) foreach ( 0 .. $#lines );
- };
- if ($@) {
- $RT::Logger->crit( "Totally failed to convert to utf-8: " . $@ . " I give up" );
- }
- }
- # }}}
+ if ( $enc ne $charset && $body ) {
+ my $string = $body->as_string or return;
+ # NOTE:: see the comments at the end of the sub.
+ Encode::_utf8_off($string);
+ my $orig_string = $string;
+
+ # {{{ Convert the body
+ eval {
+ $RT::Logger->debug( "Converting '$charset' to '$enc' for "
+ . $head->mime_type . " - "
+ . ( $head->get('subject') || 'Subjectless message' ) );
+ Encode::from_to( $string, $charset => $enc, Encode::FB_CROAK );
+ };
+
+ if ($@) {
+ $RT::Logger->error( "Encoding error: "
+ . $@
+ . " falling back to iso-8859-1 => $enc" );
+ $string = $orig_string;
+ eval {
+ Encode::from_to(
+ $string,
+ 'iso-8859-1' => $enc,
+ Encode::FB_CROAK
+ );
+ };
+ if ($@) {
+ $RT::Logger->error( "Encoding error: "
+ . $@
+ . " forcing conversion to $charset => $enc" );
+ $string = $orig_string;
+ Encode::from_to( $string, $charset => $enc );
+ }
+ }