email_parser.rb 18 KB

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