fillup_from_email.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Observer::Ticket::Article::FillupFromEmail < ActiveRecord::Observer
  3. observe 'ticket::_article'
  4. def before_create(record)
  5. # return if we run import mode
  6. return true if Setting.get('import_mode')
  7. # only do fill of email from if article got created via application_server (e. g. not
  8. # if article and sender type is set via *.postmaster)
  9. return if ApplicationHandleInfo.postmaster?
  10. # if sender is customer, do not change anything
  11. return true if !record.sender_id
  12. sender = Ticket::Article::Sender.lookup(id: record.sender_id)
  13. return true if sender.nil?
  14. return true if sender['name'] == 'Customer'
  15. # set email attributes
  16. return true if !record.type_id
  17. type = Ticket::Article::Type.lookup(id: record.type_id)
  18. return true if type['name'] != 'email'
  19. # set subject if empty
  20. ticket = record.ticket
  21. if !record.subject || record.subject == ''
  22. record.subject = ticket.title
  23. end
  24. # clean subject
  25. record.subject = ticket.subject_clean(record.subject)
  26. # generate message id, force it in prodution, in test allow to set it for testing reasons
  27. if !record.message_id || Rails.env.production?
  28. fqdn = Setting.get('fqdn')
  29. record.message_id = "<#{DateTime.current.to_s(:number)}.#{record.ticket_id}.#{rand(999_999)}@#{fqdn}>"
  30. end
  31. # generate message_id_md5
  32. record.check_message_id_md5
  33. # set sender
  34. email_address = ticket.group.email_address
  35. if !email_address
  36. raise "No email address found for group '#{ticket.group.name}' (#{ticket.group_id})"
  37. end
  38. # remember email address for background job
  39. record.preferences['email_address_id'] = email_address.id
  40. # fill from
  41. if record.created_by_id != 1 && Setting.get('ticket_define_email_from') == 'AgentNameSystemAddressName'
  42. separator = Setting.get('ticket_define_email_from_separator')
  43. sender = User.find(record.created_by_id)
  44. realname = "#{sender.firstname} #{sender.lastname} #{separator} #{email_address.realname}"
  45. record.from = Channel::EmailBuild.recipient_line(realname, email_address.email)
  46. else
  47. record.from = Channel::EmailBuild.recipient_line(email_address.realname, email_address.email)
  48. end
  49. true
  50. end
  51. end