email_parser.rb 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. # encoding: utf-8
  3. require 'mail'
  4. require 'encode'
  5. class Channel::EmailParser
  6. =begin
  7. mail = parse( msg_as_string )
  8. mail = {
  9. :from => 'Some Name <some@example.com>',
  10. :from_email => 'some@example.com',
  11. :from_local => 'some',
  12. :from_domain => 'example.com',
  13. :from_display_name => 'Some Name',
  14. :message_id => 'some_message_id@example.com',
  15. :to => 'Some System <system@example.com>',
  16. :cc => 'Somebody <somebody@example.com>',
  17. :subject => 'some message subject',
  18. :body => 'some message body',
  19. :attachments => [
  20. {
  21. :data => 'binary of attachment',
  22. :filename => 'file_name_of_attachment.txt',
  23. :preferences => {
  24. :content-alternative => true,
  25. :Mime-Type => 'text/plain',
  26. :Charset => 'iso-8859-1',
  27. },
  28. },
  29. ],
  30. # ignore email header
  31. :x-zammad-ignore => 'false',
  32. # customer headers
  33. :x-zammad-customer-login => '',
  34. :x-zammad-customer-email => '',
  35. :x-zammad-customer-firstname => '',
  36. :x-zammad-customer-lastname => '',
  37. # ticket headers
  38. :x-zammad-ticket-group => 'some_group',
  39. :x-zammad-ticket-state => 'some_state',
  40. :x-zammad-ticket-priority => 'some_priority',
  41. :x-zammad-ticket-owner => 'some_owner_login',
  42. # article headers
  43. :x-zammad-article-internal => false,
  44. :x-zammad-article-type => 'agent',
  45. :x-zammad-article-sender => 'customer',
  46. # all other email headers
  47. :some-header => 'some_value',
  48. }
  49. =end
  50. def parse (msg)
  51. data = {}
  52. mail = Mail.new( msg )
  53. # set all headers
  54. mail.header.fields.each { |field|
  55. data[field.name.to_s.downcase.to_sym] = Encode.conv( 'utf8', field.to_s )
  56. }
  57. # get sender
  58. from = nil
  59. ['from', 'reply-to', 'return-path'].each { |item|
  60. if !from
  61. if mail[ item.to_sym ]
  62. from = mail[ item.to_sym ].value
  63. end
  64. end
  65. }
  66. # set x-any-recipient
  67. data['x-any-recipient'.to_sym] = ''
  68. ['to', 'cc', 'delivered-to', 'x-original-to', 'envelope-to'].each { |item|
  69. if mail[item.to_sym]
  70. if data['x-any-recipient'.to_sym] != ''
  71. data['x-any-recipient'.to_sym] += ', '
  72. end
  73. data['x-any-recipient'.to_sym] += mail[item.to_sym].to_s
  74. end
  75. }
  76. # set extra headers
  77. begin
  78. data[:from_email] = Mail::Address.new( from ).address
  79. data[:from_local] = Mail::Address.new( from ).local
  80. data[:from_domain] = Mail::Address.new( from ).domain
  81. data[:from_display_name] = Mail::Address.new( from ).display_name ||
  82. ( Mail::Address.new( from ).comments && Mail::Address.new( from ).comments[0] )
  83. rescue
  84. data[:from_email] = from
  85. data[:from_local] = from
  86. data[:from_domain] = from
  87. end
  88. # do extra decoding because we needed to use field.value
  89. data[:from_display_name] = Mail::Field.new( 'X-From', data[:from_display_name] ).to_s
  90. # compat headers
  91. data[:message_id] = data['message-id'.to_sym]
  92. # body
  93. # plain_part = mail.multipart? ? (mail.text_part ? mail.text_part.body.decoded : nil) : mail.body.decoded
  94. # html_part = message.html_part ? message.html_part.body.decoded : nil
  95. data[:attachments] = []
  96. # multi part email
  97. if mail.multipart?
  98. # text attachment/body exists
  99. if mail.text_part
  100. data[:body] = mail.text_part.body.decoded
  101. data[:body] = Encode.conv( mail.text_part.charset, data[:body] )
  102. if !data[:body].valid_encoding?
  103. data[:body] = data[:body].encode('utf-8', 'binary', :invalid => :replace, :undef => :replace, :replace => '?')
  104. end
  105. # html attachment/body may exists and will be converted to text
  106. else
  107. filename = '-no name-'
  108. if mail.html_part.body
  109. filename = 'html-email'
  110. data[:body] = mail.html_part.body.to_s
  111. data[:body] = Encode.conv( mail.html_part.charset.to_s, data[:body] )
  112. data[:body] = html2ascii( data[:body] ).to_s.force_encoding('utf-8')
  113. if !data[:body].valid_encoding?
  114. data[:body] = data[:body].encode('utf-8', 'binary', :invalid => :replace, :undef => :replace, :replace => '?')
  115. end
  116. # any other attachments
  117. else
  118. data[:body] = 'no visible content'
  119. end
  120. end
  121. # add html attachment/body as real attachment
  122. if mail.html_part
  123. filename = 'message.html'
  124. headers_store = {
  125. 'content-alternative' => true,
  126. }
  127. if mail.mime_type
  128. headers_store['Mime-Type'] = mail.html_part.mime_type
  129. end
  130. if mail.charset
  131. headers_store['Charset'] = mail.html_part.charset
  132. end
  133. attachment = {
  134. :data => mail.html_part.body.to_s,
  135. :filename => mail.html_part.filename || filename,
  136. :preferences => headers_store
  137. }
  138. data[:attachments].push attachment
  139. end
  140. # get attachments
  141. if mail.parts
  142. attachment_count_total = 0
  143. mail.parts.each { |part|
  144. attachment_count_total += 1
  145. # protect process to work fine with spam emails, see test/fixtures/mail15.box
  146. begin
  147. if mail.text_part && mail.text_part == part
  148. # ignore text/plain attachments - already shown in view
  149. elsif mail.html_part && mail.html_part == part
  150. # ignore text/html - html part, already shown in view
  151. else
  152. attachs = self._get_attachment( part, data[:attachments] )
  153. data[:attachments].concat( attachs )
  154. end
  155. rescue
  156. attachs = self._get_attachment( part, data[:attachments] )
  157. data[:attachments].concat( attachs )
  158. end
  159. }
  160. end
  161. # not multipart email
  162. else
  163. # text part
  164. if !mail.mime_type || mail.mime_type.to_s == '' || mail.mime_type.to_s.downcase == 'text/plain'
  165. data[:body] = mail.body.decoded
  166. data[:body] = Encode.conv( mail.charset, data[:body] )
  167. if !data[:body].valid_encoding?
  168. data[:body] = data[:body].encode('utf-8', 'binary', :invalid => :replace, :undef => :replace, :replace => '?')
  169. end
  170. # html part
  171. else
  172. filename = '-no name-'
  173. if mail.mime_type.to_s.downcase == 'text/html'
  174. filename = 'html-email'
  175. data[:body] = mail.body.decoded
  176. data[:body] = Encode.conv( mail.charset, data[:body] )
  177. data[:body] = html2ascii( data[:body] ).to_s.force_encoding('utf-8')
  178. if !data[:body].valid_encoding?
  179. data[:body] = data[:body].encode('utf-8', 'binary', :invalid => :replace, :undef => :replace, :replace => '?')
  180. end
  181. # any other attachments
  182. else
  183. data[:body] = 'no visible content'
  184. end
  185. # add body as attachment
  186. headers_store = {
  187. 'content-alternative' => true,
  188. }
  189. if mail.mime_type
  190. headers_store['Mime-Type'] = mail.mime_type
  191. end
  192. if mail.charset
  193. headers_store['Charset'] = mail.charset
  194. end
  195. attachment = {
  196. :data => mail.body.decoded,
  197. :filename => mail.filename || filename,
  198. :preferences => headers_store
  199. }
  200. data[:attachments].push attachment
  201. end
  202. end
  203. # strip not wanted chars
  204. data[:body].gsub!( /\r\n/, "\n" )
  205. data[:body].gsub!( /\r/, "\n" )
  206. return data
  207. end
  208. def _get_attachment( file, attachments )
  209. # check if sub parts are available
  210. if !file.parts.empty?
  211. a = []
  212. file.parts.each {|p|
  213. a.concat( self._get_attachment( p, attachments ) )
  214. }
  215. return a
  216. end
  217. # get file preferences
  218. headers_store = {}
  219. file.header.fields.each { |field|
  220. headers_store[field.name.to_s] = field.value.to_s
  221. }
  222. # get filename from content-disposition
  223. filename = nil
  224. # workaround for: NoMethodError: undefined method `filename' for #<Mail::UnstructuredField:0x007ff109e80678>
  225. begin
  226. filename = file.header[:content_disposition].filename
  227. rescue
  228. result = file.header[:content_disposition].to_s.scan( /filename=("|)(.+?)("|);/i )
  229. if result && result[0] && result[0][1]
  230. filename = result[0][1]
  231. end
  232. end
  233. # for some broken sm mail clients (X-MimeOLE: Produced By Microsoft Exchange V6.5)
  234. if !filename
  235. filename = file.header[:content_location].to_s
  236. end
  237. # generate file name
  238. if !filename || filename.empty?
  239. attachment_count = 0
  240. (1..1000).each {|count|
  241. filename_exists = false
  242. filename = 'file-' + count.to_s
  243. attachments.each {|attachment|
  244. if attachment[:filename] == filename
  245. filename_exists = true
  246. end
  247. }
  248. break if filename_exists == false
  249. }
  250. end
  251. # get mime type
  252. if file.header[:content_type] && file.header[:content_type].string
  253. headers_store['Mime-Type'] = file.header[:content_type].string
  254. end
  255. # get charset
  256. if file.header && file.header.charset
  257. headers_store['Charset'] = file.header.charset
  258. end
  259. # remove not needed header
  260. headers_store.delete('Content-Transfer-Encoding')
  261. headers_store.delete('Content-Disposition')
  262. attach = {
  263. :data => file.body.to_s,
  264. :filename => filename,
  265. :preferences => headers_store,
  266. }
  267. return [attach]
  268. end
  269. def process(channel, msg)
  270. mail = parse( msg )
  271. # run postmaster pre filter
  272. filters = {
  273. '0010' => Channel::Filter::Trusted,
  274. '1000' => Channel::Filter::Database,
  275. }
  276. # filter( channel, mail )
  277. filters.each {|prio, backend|
  278. begin
  279. backend.run( channel, mail )
  280. rescue Exception => e
  281. puts "can't run postmaster pre filter #{backend}"
  282. puts e.inspect
  283. return false
  284. end
  285. }
  286. # check ignore header
  287. return true if mail[ 'x-zammad-ignore'.to_sym ] == 'true' || mail[ 'x-zammad-ignore'.to_sym ] == true
  288. ticket = nil
  289. article = nil
  290. user = nil
  291. # use transaction
  292. ActiveRecord::Base.transaction do
  293. # reset current_user
  294. UserInfo.current_user_id = 1
  295. if mail[ 'x-zammad-customer-login'.to_sym ]
  296. user = User.where( :login => mail[ 'x-zammad-customer-login'.to_sym ] ).first
  297. end
  298. if !user
  299. user = User.where( :email => mail[ 'x-zammad-customer-email'.to_sym ] || mail[:from_email] ).first
  300. end
  301. if !user
  302. puts 'create user...'
  303. roles = Role.where( :name => 'Customer' )
  304. user = User.create(
  305. :login => mail[ 'x-zammad-customer-login'.to_sym ] || mail[ 'x-zammad-customer-email'.to_sym ] || mail[:from_email],
  306. :firstname => mail[ 'x-zammad-customer-firstname'.to_sym ] || mail[:from_display_name],
  307. :lastname => mail[ 'x-zammad-customer-lastname'.to_sym ],
  308. :email => mail[ 'x-zammad-customer-email'.to_sym ] || mail[:from_email],
  309. :password => '',
  310. :active => true,
  311. :roles => roles,
  312. :updated_by_id => 1,
  313. :created_by_id => 1,
  314. )
  315. end
  316. # set current user
  317. UserInfo.current_user_id = user.id
  318. # get ticket# from subject
  319. ticket = Ticket::Number.check( mail[:subject] )
  320. # set ticket state to open if not new
  321. if ticket
  322. state = Ticket::State.find( ticket.state_id )
  323. state_type = Ticket::StateType.find( state.state_type_id )
  324. # if tickte is merged, find linked ticket
  325. if state_type.name == 'merged'
  326. end
  327. if state_type.name != 'new'
  328. ticket.state = Ticket::State.where( :name => 'open' ).first
  329. ticket.save
  330. end
  331. end
  332. # create new ticket
  333. if !ticket
  334. # set attributes
  335. ticket = Ticket.new(
  336. :group_id => channel[:group_id] || 1,
  337. :customer_id => user.id,
  338. :title => mail[:subject] || '',
  339. :state_id => Ticket::State.where( :name => 'new' ).first.id,
  340. :priority_id => Ticket::Priority.where( :name => '2 normal' ).first.id,
  341. )
  342. set_attributes_by_x_headers( ticket, 'ticket', mail )
  343. # create ticket
  344. ticket.save
  345. end
  346. # import mail
  347. # set attributes
  348. article = Ticket::Article.new(
  349. :ticket_id => ticket.id,
  350. :type_id => Ticket::Article::Type.where( :name => 'email' ).first.id,
  351. :sender_id => Ticket::Article::Sender.where( :name => 'Customer' ).first.id,
  352. :body => mail[:body],
  353. :from => mail[:from],
  354. :to => mail[:to],
  355. :cc => mail[:cc],
  356. :subject => mail[:subject],
  357. :message_id => mail[:message_id],
  358. :internal => false,
  359. )
  360. # x-headers lookup
  361. set_attributes_by_x_headers( article, 'article', mail )
  362. # create article
  363. article.save
  364. # store mail plain
  365. Store.add(
  366. :object => 'Ticket::Article::Mail',
  367. :o_id => article.id,
  368. :data => msg,
  369. :filename => "ticket-#{ticket.number}-#{article.id}.eml",
  370. :preferences => {}
  371. )
  372. # store attachments
  373. if mail[:attachments]
  374. mail[:attachments].each do |attachment|
  375. Store.add(
  376. :object => 'Ticket::Article',
  377. :o_id => article.id,
  378. :data => attachment[:data],
  379. :filename => attachment[:filename],
  380. :preferences => attachment[:preferences]
  381. )
  382. end
  383. end
  384. end
  385. # execute ticket events
  386. Observer::Ticket::Notification.transaction
  387. # run postmaster post filter
  388. filters = {
  389. # '0010' => Channel::Filter::Trusted,
  390. }
  391. # filter( channel, mail )
  392. filters.each {|prio, backend|
  393. begin
  394. backend.run( channel, mail, ticket, article, user )
  395. rescue Exception => e
  396. puts "can't run postmaster post filter #{backend}"
  397. puts e.inspect
  398. end
  399. }
  400. # return new objects
  401. return ticket, article, user
  402. end
  403. def set_attributes_by_x_headers( item_object, header_name, mail )
  404. # loop all x-zammad-hedaer-* headers
  405. item_object.attributes.each{|key,value|
  406. # ignore read only attributes
  407. next if key == 'updated_at'
  408. next if key == 'created_at'
  409. next if key == 'updated_by_id'
  410. next if key == 'created_by_id'
  411. # check if id exists
  412. key_short = key[ key.length-3 , key.length ]
  413. if key_short == '_id'
  414. key_short = key[ 0, key.length-3 ]
  415. header = "x-zammad-#{header_name}-#{key_short}"
  416. if mail[ header.to_sym ]
  417. puts "NOTICE: header #{header} found #{mail[ header.to_sym ]}"
  418. item_object.class.reflect_on_all_associations.map { |assoc|
  419. if assoc.name.to_s == key_short
  420. puts "NOTICE: ASSOC found #{assoc.class_name} lookup #{mail[ header.to_sym ]}"
  421. item = assoc.class_name.constantize
  422. if item.respond_to?(:name)
  423. if item.lookup( :name => mail[ header.to_sym ] )
  424. item_object[key] = item.lookup( :name => mail[ header.to_sym ] ).id
  425. end
  426. elsif item.respond_to?(:login)
  427. if item.lookup( :login => mail[ header.to_sym ] )
  428. item_object[key] = item.lookup( :login => mail[ header.to_sym ] ).id
  429. end
  430. end
  431. end
  432. }
  433. end
  434. end
  435. # check if attribute exists
  436. header = "x-zammad-#{header_name}-#{key}"
  437. if mail[ header.to_sym ]
  438. puts "NOTICE: header #{header} found #{mail[ header.to_sym ]}"
  439. item_object[key] = mail[ header.to_sym ]
  440. end
  441. }
  442. end
  443. def html2ascii(string)
  444. # in case of invalid encodeing, strip invalid chars
  445. # see also test/fixtures/mail21.box
  446. # note: string.encode!('UTF-8', 'UTF-8', :invalid => :replace, :replace => '?') was not detecting invalid chars
  447. if !string.valid_encoding?
  448. string = string.chars.select { |c| c.valid_encoding? }.join
  449. end
  450. # find <a href=....> and replace it with [x]
  451. link_list = ''
  452. counter = 0
  453. string.gsub!( /<a\s.*?href=("|')(.+?)("|').*?>/ix ) { |item|
  454. link = $2
  455. counter = counter + 1
  456. link_list += "[#{counter}] #{link}\n"
  457. "[#{counter}]"
  458. }
  459. # remove empty lines
  460. string.gsub!( /^\s*/m, '' )
  461. # fix some bad stuff from opera and others
  462. string.gsub!( /(\n\r|\r\r\n|\r\n)/, "\n" )
  463. # strip all other tags
  464. string.gsub!( /\<(br|br\/|br\s\/)\>/, "\n" )
  465. # strip all other tags
  466. string.gsub!( /\<.+?\>/, '' )
  467. # strip all &amp; &lt; &gt; &quot;
  468. string.gsub!( '&amp;', '&' )
  469. string.gsub!( '&lt;', '<' )
  470. string.gsub!( '&gt;', '>' )
  471. string.gsub!( '&quot;', '"' )
  472. # encode html entities like "&#8211;"
  473. string.gsub!( /(&\#(\d+);?)/x ) { |item|
  474. $2.chr
  475. }
  476. # encode html entities like "&#3d;"
  477. string.gsub!( /(&\#[xX]([0-9a-fA-F]+);?)/x ) { |item|
  478. chr_orig = $1
  479. hex = $2.hex
  480. if hex
  481. chr = hex.chr
  482. if chr
  483. chr_orig = chr
  484. else
  485. chr_orig
  486. end
  487. else
  488. chr_orig
  489. end
  490. # check valid encoding
  491. begin
  492. if !chr_orig.encode('UTF-8').valid_encoding?
  493. chr_orig = '?'
  494. end
  495. rescue
  496. chr_orig = '?'
  497. end
  498. chr_orig
  499. }
  500. # remove empty lines
  501. string.gsub!( /^\s*\n\s*\n/m, "\n" )
  502. # add extracted links
  503. if link_list
  504. string += "\n\n" + link_list
  505. end
  506. return string
  507. end
  508. end
  509. # workaround to parse subjects with 2 different encodings correctly (e. g. quoted-printable see test/fixtures/mail9.box)
  510. module Mail
  511. module Encodings
  512. def Encodings.value_decode(str)
  513. # Optimization: If there's no encoded-words in the string, just return it
  514. return str unless str.index("=?")
  515. str = str.gsub(/\?=(\s*)=\?/, '?==?') # Remove whitespaces between 'encoded-word's
  516. # Split on white-space boundaries with capture, so we capture the white-space as well
  517. str.split(/([ \t])/).map do |text|
  518. if text.index('=?') .nil?
  519. text
  520. else
  521. # Join QP encoded-words that are adjacent to avoid decoding partial chars
  522. # text.gsub!(/\?\=\=\?.+?\?[Qq]\?/m, '') if text =~ /\?==\?/
  523. # Search for occurences of quoted strings or plain strings
  524. text.scan(/( # Group around entire regex to include it in matches
  525. \=\?[^?]+\?([QB])\?[^?]+?\?\= # Quoted String with subgroup for encoding method
  526. | # or
  527. .+?(?=\=\?|$) # Plain String
  528. )/xmi).map do |matches|
  529. string, method = *matches
  530. if method == 'b' || method == 'B'
  531. b_value_decode(string)
  532. elsif method == 'q' || method == 'Q'
  533. q_value_decode(string)
  534. else
  535. string
  536. end
  537. end
  538. end
  539. end.join("")
  540. end
  541. end
  542. end