fillup_from_email.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright (C) 2012-2014 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 if Setting.get('import_mode')
  7. # if sender is customer, do not change anything
  8. sender = Ticket::Article::Sender.lookup( :id => record.sender_id )
  9. return if sender == nil
  10. return if sender['name'] == 'Customer'
  11. # set email attributes
  12. type = Ticket::Article::Type.lookup( :id => record.type_id )
  13. return if type['name'] != 'email'
  14. # set subject if empty
  15. ticket = Ticket.lookup( :id => record.ticket_id )
  16. if !record.subject || record.subject == ''
  17. record.subject = ticket.title
  18. end
  19. # clean subject
  20. record.subject = ticket.subject_clean( record.subject )
  21. # generate message id
  22. fqdn = Setting.get('fqdn')
  23. record.message_id = '<' + DateTime.current.to_s(:number) + '.' + record.ticket_id.to_s + '.' + rand(999999).to_s() + '@' + fqdn + '>'
  24. # set sender
  25. email_address = ticket.group.email_address
  26. if !email_address
  27. raise "No email address found for group '#{ticket.group.name}'"
  28. end
  29. system_sender = "#{email_address.realname} <#{email_address.email}>"
  30. if Setting.get('ticket_define_email_from') == 'AgentNameSystemAddressName'
  31. seperator = Setting.get('ticket_define_email_from_seperator')
  32. sender = User.find( record.created_by_id )
  33. record.from = "#{sender.firstname} #{sender.lastname} #{seperator} #{system_sender}"
  34. else
  35. record.from = system_sender
  36. end
  37. end
  38. end