email_parser.rb 18 KB

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