From 90628fb67a66a91164f67fdf98aa212ea8fb872f Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 4 Oct 2012 08:54:21 +0200 Subject: [PATCH] Renamed plain_part to body in email parser key for message body. --- app/models/channel/email_parser.rb | 65 +++++++++++++++++++----------- test/unit/email_parser_test.rb | 20 ++++----- 2 files changed, 52 insertions(+), 33 deletions(-) diff --git a/app/models/channel/email_parser.rb b/app/models/channel/email_parser.rb index 1d3454461..3db850afb 100644 --- a/app/models/channel/email_parser.rb +++ b/app/models/channel/email_parser.rb @@ -13,7 +13,27 @@ class Channel::EmailParser # string.encode("UTF-8") Iconv.conv( 'UTF8', charset, string ) end - + +=begin + + mail = parse( msg_as_string ) + + mail = { + :from => 'Some Name ', + :from_email => 'some@example.com', + :from_local => 'some', + :from_domain => 'example.com', + :from_display_name => 'Some Name', + :message_id => 'some_message_id@example.com', + :body => 'message body', + :attachments => [ + + ], + + } + +=end + def parse (msg) data = {} mail = Mail.new( msg ) @@ -46,21 +66,21 @@ class Channel::EmailParser # text attachment/body exists if mail.text_part - data[:plain_part] = mail.text_part.body.decoded - data[:plain_part] = conv( mail.text_part.charset, data[:plain_part] ) + data[:body] = mail.text_part.body.decoded + data[:body] = conv( mail.text_part.charset, data[:body] ) # html attachment/body may exists and will be converted to text else filename = '-no name-' if mail.html_part.body filename = 'html-email' - data[:plain_part] = mail.html_part.body.to_s - data[:plain_part] = conv( mail.html_part.charset.to_s, data[:plain_part] ) - data[:plain_part] = html2ascii( data[:plain_part] ) + data[:body] = mail.html_part.body.to_s + data[:body] = conv( mail.html_part.charset.to_s, data[:body] ) + data[:body] = html2ascii( data[:body] ) # any other attachments else - data[:plain_part] = 'no visible content' + data[:body] = 'no visible content' end end @@ -79,11 +99,11 @@ class Channel::EmailParser attachment = { :data => mail.html_part.body.to_s, :filename => mail.html_part.filename || filename, - :preferences => headers_store + :preferences => headers_store } data[:attachments].push attachment end - + # get attachments if mail.has_attachments? mail.attachments.each { |file| @@ -103,7 +123,7 @@ class Channel::EmailParser if file.header.charset headers_store['Charset'] = file.header.charset end - + # remove not needed header headers_store.delete('Content-Transfer-Encoding') headers_store.delete('Content-Disposition') @@ -111,9 +131,8 @@ class Channel::EmailParser attach = { :data => file.body.to_s, :filename => filename, - :preferences => headers_store + :preferences => headers_store, } - data[:attachments].push attach } end @@ -123,21 +142,21 @@ class Channel::EmailParser # text part if !mail.mime_type || mail.mime_type.to_s == '' || mail.mime_type.to_s.downcase == 'text/plain' - data[:plain_part] = mail.body.decoded - data[:plain_part] = conv( mail.charset, data[:plain_part] ) + data[:body] = mail.body.decoded + data[:body] = conv( mail.charset, data[:body] ) # html part else filename = '-no name-' if mail.mime_type.to_s.downcase == 'text/html' filename = 'html-email' - data[:plain_part] = mail.body.decoded - data[:plain_part] = conv( mail.charset, data[:plain_part] ) - data[:plain_part] = html2ascii( data[:plain_part] ) + data[:body] = mail.body.decoded + data[:body] = conv( mail.charset, data[:body] ) + data[:body] = html2ascii( data[:body] ) # any other attachments else - data[:plain_part] = 'no visible content' + data[:body] = 'no visible content' end # add body as attachment @@ -160,8 +179,8 @@ class Channel::EmailParser end # strip not wanted chars - data[:plain_part].gsub!( /\r\n/, "\n" ) - data[:plain_part].gsub!( /\r/, "\n" ) + data[:body].gsub!( /\r\n/, "\n" ) + data[:body].gsub!( /\r/, "\n" ) return data end @@ -262,9 +281,9 @@ class Channel::EmailParser # create ticket ticket = Ticket.create( ticket_attributes ) end - + # import mail - + # set attributes internal = false if mail[ 'X-Zammad-Article-Visability'.to_sym ] && mail[ 'X-Zammad-Article-Visability'.to_sym ] == 'internal' @@ -275,7 +294,7 @@ class Channel::EmailParser :ticket_id => ticket.id, :ticket_article_type_id => Ticket::Article::Type.where( :name => 'email' ).first.id, :ticket_article_sender_id => Ticket::Article::Sender.where( :name => 'Customer' ).first.id, - :body => mail[:plain_part], + :body => mail[:body], :from => mail[:from], :to => mail[:to], :cc => mail[:cc], diff --git a/test/unit/email_parser_test.rb b/test/unit/email_parser_test.rb index c0b6d7bf4..38ef55aa4 100644 --- a/test/unit/email_parser_test.rb +++ b/test/unit/email_parser_test.rb @@ -22,7 +22,7 @@ class EmailParserTest < ActiveSupport::TestCase :from_email => 'martin@example.com', :from_display_name => 'Martin Edenhofer', :subject => 'aaäöüßad asd', - :plain_part_md5 => "äöüß ad asd\n\n-Martin\n\n--\nOld programmers never die. They just branch to a new address.", + :body_md5 => "äöüß ad asd\n\n-Martin\n\n--\nOld programmers never die. They just branch to a new address.", }, }, { @@ -43,7 +43,7 @@ class EmailParserTest < ActiveSupport::TestCase :from_email => 'k.guenther@example.com', :from_display_name => 'Günther Katja | Example GmbH', :subject => 'AW: Ticket Templates [Ticket#11168]', - :plain_part_md5 => "Hallo Katja, + :body_md5 => "Hallo Katja, super! Ich freu mich! @@ -82,7 +82,7 @@ Liebe Grüße! :from_email => 'me@bogen.net', :from_display_name => 'Hans BÄKOSchönland', :subject => 'utf8: 使って / ISO-8859-1: Priorität" / cp-1251: Сергей Углицких', - :plain_part => "this is a test [1]Compare Cable, DSL or Satellite plans: As low as $2.95. + :body => "this is a test [1]Compare Cable, DSL or Satellite plans: As low as $2.95. Test1:8 @@ -107,7 +107,7 @@ Test5:= :from_email => 'Eike.Ehringer@example.com', :from_display_name => nil, :subject => 'AW:Installation [Ticket#11392]', - :plain_part_md5 => "Hallo. + :body_md5 => "Hallo. Jetzt muss ich dir noch kurzfristig absagen für morgen. Lass uns evtl morgen Tel. @@ -149,7 +149,7 @@ Managing Director: Martin Edenhofer :from_email => 'Franz.Schaefer@example.com', :from_display_name => nil, :subject => 'could not rename: ZZZAAuto', - :plain_part_md5 => "Gravierend? + :body_md5 => "Gravierend? Mit freundlichen Grüßen @@ -192,7 +192,7 @@ Hof", :from_email => 'martin@example.de', :from_display_name => 'Martin Edenhofer', :subject => 'AW: OTRS / Anfrage OTRS Einführung/Präsentation [Ticket#11545]', - :plain_part => "Enjoy!\n\n-Martin\n\n--\nOld programmers never die. They just branch to a new address." + :body => "Enjoy!\n\n-Martin\n\n--\nOld programmers never die. They just branch to a new address." }, }, ] @@ -203,16 +203,16 @@ Hof", data = parser.parse( file[:data] ) # check body - md5 = Digest::MD5.hexdigest( data[:plain_part] ) + md5 = Digest::MD5.hexdigest( data[:body] ) assert_equal( file[:body_md5], md5 ) # check params file[:params].each { |key, value| - if key.to_s == 'plain_part_md5' + if key.to_s == 'body_md5' # puts 'md5' -# puts '++' + data[:plain_part].to_s + '++' +# puts '++' + data[:body].to_s + '++' # puts '++' + file[:params][key.to_sym].to_s + '++' - assert_equal( Digest::MD5.hexdigest( file[:params][key.to_sym].to_s ), Digest::MD5.hexdigest( data[:plain_part].to_s ) ) + assert_equal( Digest::MD5.hexdigest( file[:params][key.to_sym].to_s ), Digest::MD5.hexdigest( data[:body].to_s ) ) else assert_equal( file[:params][key.to_sym], data[key.to_sym] ) end