Renamed plain_part to body in email parser key for message body.

This commit is contained in:
Martin Edenhofer 2012-10-04 08:54:21 +02:00
parent b8b02ca845
commit 90628fb67a
2 changed files with 52 additions and 33 deletions

View file

@ -13,7 +13,27 @@ class Channel::EmailParser
# string.encode("UTF-8") # string.encode("UTF-8")
Iconv.conv( 'UTF8', charset, string ) Iconv.conv( 'UTF8', charset, string )
end end
=begin
mail = parse( msg_as_string )
mail = {
:from => 'Some Name <some@example.com>',
: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) def parse (msg)
data = {} data = {}
mail = Mail.new( msg ) mail = Mail.new( msg )
@ -46,21 +66,21 @@ class Channel::EmailParser
# text attachment/body exists # text attachment/body exists
if mail.text_part if mail.text_part
data[:plain_part] = mail.text_part.body.decoded data[:body] = mail.text_part.body.decoded
data[:plain_part] = conv( mail.text_part.charset, data[:plain_part] ) data[:body] = conv( mail.text_part.charset, data[:body] )
# html attachment/body may exists and will be converted to text # html attachment/body may exists and will be converted to text
else else
filename = '-no name-' filename = '-no name-'
if mail.html_part.body if mail.html_part.body
filename = 'html-email' filename = 'html-email'
data[:plain_part] = mail.html_part.body.to_s data[:body] = mail.html_part.body.to_s
data[:plain_part] = conv( mail.html_part.charset.to_s, data[:plain_part] ) data[:body] = conv( mail.html_part.charset.to_s, data[:body] )
data[:plain_part] = html2ascii( data[:plain_part] ) data[:body] = html2ascii( data[:body] )
# any other attachments # any other attachments
else else
data[:plain_part] = 'no visible content' data[:body] = 'no visible content'
end end
end end
@ -79,11 +99,11 @@ class Channel::EmailParser
attachment = { attachment = {
:data => mail.html_part.body.to_s, :data => mail.html_part.body.to_s,
:filename => mail.html_part.filename || filename, :filename => mail.html_part.filename || filename,
:preferences => headers_store :preferences => headers_store
} }
data[:attachments].push attachment data[:attachments].push attachment
end end
# get attachments # get attachments
if mail.has_attachments? if mail.has_attachments?
mail.attachments.each { |file| mail.attachments.each { |file|
@ -103,7 +123,7 @@ class Channel::EmailParser
if file.header.charset if file.header.charset
headers_store['Charset'] = file.header.charset headers_store['Charset'] = file.header.charset
end end
# remove not needed header # remove not needed header
headers_store.delete('Content-Transfer-Encoding') headers_store.delete('Content-Transfer-Encoding')
headers_store.delete('Content-Disposition') headers_store.delete('Content-Disposition')
@ -111,9 +131,8 @@ class Channel::EmailParser
attach = { attach = {
:data => file.body.to_s, :data => file.body.to_s,
:filename => filename, :filename => filename,
:preferences => headers_store :preferences => headers_store,
} }
data[:attachments].push attach data[:attachments].push attach
} }
end end
@ -123,21 +142,21 @@ class Channel::EmailParser
# text part # text part
if !mail.mime_type || mail.mime_type.to_s == '' || mail.mime_type.to_s.downcase == 'text/plain' if !mail.mime_type || mail.mime_type.to_s == '' || mail.mime_type.to_s.downcase == 'text/plain'
data[:plain_part] = mail.body.decoded data[:body] = mail.body.decoded
data[:plain_part] = conv( mail.charset, data[:plain_part] ) data[:body] = conv( mail.charset, data[:body] )
# html part # html part
else else
filename = '-no name-' filename = '-no name-'
if mail.mime_type.to_s.downcase == 'text/html' if mail.mime_type.to_s.downcase == 'text/html'
filename = 'html-email' filename = 'html-email'
data[:plain_part] = mail.body.decoded data[:body] = mail.body.decoded
data[:plain_part] = conv( mail.charset, data[:plain_part] ) data[:body] = conv( mail.charset, data[:body] )
data[:plain_part] = html2ascii( data[:plain_part] ) data[:body] = html2ascii( data[:body] )
# any other attachments # any other attachments
else else
data[:plain_part] = 'no visible content' data[:body] = 'no visible content'
end end
# add body as attachment # add body as attachment
@ -160,8 +179,8 @@ class Channel::EmailParser
end end
# strip not wanted chars # strip not wanted chars
data[:plain_part].gsub!( /\r\n/, "\n" ) data[:body].gsub!( /\r\n/, "\n" )
data[:plain_part].gsub!( /\r/, "\n" ) data[:body].gsub!( /\r/, "\n" )
return data return data
end end
@ -262,9 +281,9 @@ class Channel::EmailParser
# create ticket # create ticket
ticket = Ticket.create( ticket_attributes ) ticket = Ticket.create( ticket_attributes )
end end
# import mail # import mail
# set attributes # set attributes
internal = false internal = false
if mail[ 'X-Zammad-Article-Visability'.to_sym ] && mail[ 'X-Zammad-Article-Visability'.to_sym ] == 'internal' 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_id => ticket.id,
:ticket_article_type_id => Ticket::Article::Type.where( :name => 'email' ).first.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, :ticket_article_sender_id => Ticket::Article::Sender.where( :name => 'Customer' ).first.id,
:body => mail[:plain_part], :body => mail[:body],
:from => mail[:from], :from => mail[:from],
:to => mail[:to], :to => mail[:to],
:cc => mail[:cc], :cc => mail[:cc],

View file

@ -22,7 +22,7 @@ class EmailParserTest < ActiveSupport::TestCase
:from_email => 'martin@example.com', :from_email => 'martin@example.com',
:from_display_name => 'Martin Edenhofer', :from_display_name => 'Martin Edenhofer',
:subject => 'aaäöüßad asd', :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_email => 'k.guenther@example.com',
:from_display_name => 'Günther Katja | Example GmbH', :from_display_name => 'Günther Katja | Example GmbH',
:subject => 'AW: Ticket Templates [Ticket#11168]', :subject => 'AW: Ticket Templates [Ticket#11168]',
:plain_part_md5 => "Hallo Katja, :body_md5 => "Hallo Katja,
super! Ich freu mich! super! Ich freu mich!
@ -82,7 +82,7 @@ Liebe Grüße!
:from_email => 'me@bogen.net', :from_email => 'me@bogen.net',
:from_display_name => 'Hans BÄKOSchönland', :from_display_name => 'Hans BÄKOSchönland',
:subject => 'utf8: 使って / ISO-8859-1: Priorität" / cp-1251: Сергей Углицких', :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 Test1:8
@ -107,7 +107,7 @@ Test5:=
:from_email => 'Eike.Ehringer@example.com', :from_email => 'Eike.Ehringer@example.com',
:from_display_name => nil, :from_display_name => nil,
:subject => 'AW:Installation [Ticket#11392]', :subject => 'AW:Installation [Ticket#11392]',
:plain_part_md5 => "Hallo. :body_md5 => "Hallo.
Jetzt muss ich dir noch kurzfristig absagen für morgen. Jetzt muss ich dir noch kurzfristig absagen für morgen.
Lass uns evtl morgen Tel. Lass uns evtl morgen Tel.
@ -149,7 +149,7 @@ Managing Director: Martin Edenhofer
:from_email => 'Franz.Schaefer@example.com', :from_email => 'Franz.Schaefer@example.com',
:from_display_name => nil, :from_display_name => nil,
:subject => 'could not rename: ZZZAAuto', :subject => 'could not rename: ZZZAAuto',
:plain_part_md5 => "Gravierend? :body_md5 => "Gravierend?
Mit freundlichen Grüßen Mit freundlichen Grüßen
@ -192,7 +192,7 @@ Hof",
:from_email => 'martin@example.de', :from_email => 'martin@example.de',
:from_display_name => 'Martin Edenhofer', :from_display_name => 'Martin Edenhofer',
:subject => 'AW: OTRS / Anfrage OTRS Einführung/Präsentation [Ticket#11545]', :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] ) data = parser.parse( file[:data] )
# check body # check body
md5 = Digest::MD5.hexdigest( data[:plain_part] ) md5 = Digest::MD5.hexdigest( data[:body] )
assert_equal( file[:body_md5], md5 ) assert_equal( file[:body_md5], md5 )
# check params # check params
file[:params].each { |key, value| file[:params].each { |key, value|
if key.to_s == 'plain_part_md5' if key.to_s == 'body_md5'
# puts 'md5' # puts 'md5'
# puts '++' + data[:plain_part].to_s + '++' # puts '++' + data[:body].to_s + '++'
# puts '++' + file[:params][key.to_sym].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 else
assert_equal( file[:params][key.to_sym], data[key.to_sym] ) assert_equal( file[:params][key.to_sym], data[key.to_sym] )
end end