diff --git a/app/models/channel/email_parser.rb b/app/models/channel/email_parser.rb index 1cebc0d15..08a3b02b4 100644 --- a/app/models/channel/email_parser.rb +++ b/app/models/channel/email_parser.rb @@ -1,17 +1,41 @@ # encoding: utf-8 require 'mail' -require 'iconv' +#require 'iconv' class Channel::EmailParser def conv (charset, string) + + # if no charset is given, use LATIN1 as default if !charset || charset == 'US-ASCII' || charset == 'ASCII-8BIT' charset = 'LATIN1' end - return string if charset.downcase == 'utf8' || charset.downcase == 'utf-8' + + # return if string is false + return string if !string + + # validate already existing utf8 strings + if charset.downcase == 'utf8' || charset.downcase == 'utf-8' + begin + + # return if encoding is valid + utf8 = string.force_encoding('UTF-8') + return utf8 if utf8.valid_encoding? + + # try to encode from Windows-1252 to utf8 + string.encode!( 'UTF-8', 'Windows-1252' ) + + rescue EncodingError => e + puts "Bad encoding: #{new_value.inspect}" + string.encode!( 'UTF-8', invalid: :replace, undef: :replace, replace: '?' ) + end + return string + end # puts '-------' + charset # puts string -# string.encode("UTF-8") - Iconv.conv( 'UTF8', charset, string ) + + # convert string + string.encode!( 'UTF-8', charset.upcase ) +# Iconv.conv( 'UTF8', charset, string ) end =begin @@ -73,7 +97,7 @@ class Channel::EmailParser # set all headers mail.header.fields.each { |field| - data[field.name.downcase.to_sym] = field.to_s + data[field.name.downcase.to_sym] = conv( 'utf8', field.to_s ) } # set extra headers diff --git a/test/unit/email_process_test.rb b/test/unit/email_process_test.rb index b99200ba7..51cbd342d 100644 --- a/test/unit/email_process_test.rb +++ b/test/unit/email_process_test.rb @@ -34,6 +34,7 @@ Some Text', :result => { 0 => { :ticket_priority => '3 high', + :title => 'some subject', }, 1 => { :ticket_article_sender => 'System', @@ -41,6 +42,25 @@ Some Text', }, }, }, + { + :data => "From: me@example.com +To: customer@example.com +Subject: äöü some subject + +Some Textäöü".encode("ISO-8859-1"), + :success => true, + :result => { + 0 => { + :ticket_priority => '2 normal', + :title => 'äöü some subject', + }, + 1 => { + :body => 'Some Textäöü', + :ticket_article_sender => 'Customer', + :ticket_article_type => 'email', + }, + }, + }, ] files.each { |file| @@ -52,7 +72,11 @@ Some Text', [ 0, 1, 2 ].each { |level| if file[:result][level] file[:result][level].each { |key, value| - assert_equal( result[level].send(key).name, value.to_s) + if result[level].send(key).respond_to?('name') + assert_equal( result[level].send(key).name, value.to_s) + else + assert_equal( result[level].send(key), value.to_s) + end } end }