adds_metadata_email.rb 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. # Adds certain (missing) meta data when creating email articles.
  3. module Ticket::Article::AddsMetadataEmail
  4. extend ActiveSupport::Concern
  5. included do
  6. before_create :ticket_article_add_metadata_email
  7. end
  8. private
  9. def ticket_article_add_metadata_email
  10. return if !neither_importing_nor_postmaster?
  11. return if !sender_needs_metadata?
  12. return if !type_needs_metadata?
  13. metadata_email_process_subject
  14. metadata_email_process_message_id
  15. metadata_email_process_email_address
  16. metadata_email_process_from
  17. end
  18. def sender_needs_metadata?
  19. return if !sender_id
  20. sender = Ticket::Article::Sender.lookup(id: sender_id)
  21. return if sender&.name == 'Customer'
  22. true
  23. end
  24. def type_needs_metadata?
  25. return if !type_id
  26. type = Ticket::Article::Type.lookup(id: type_id)
  27. return if type&.name != 'email'
  28. true
  29. end
  30. def metadata_email_process_subject
  31. self.subject = ticket.subject_clean subject.presence || ticket.title
  32. end
  33. def metadata_email_process_message_id
  34. # generate message id, force it in production, in test allow to set it for testing reasons
  35. if !message_id || Rails.env.production?
  36. fqdn = Setting.get('fqdn')
  37. self.message_id = "<#{DateTime.current.to_fs(:number)}.#{ticket_id}.#{SecureRandom.uuid}@#{fqdn}>"
  38. end
  39. # generate message_id_md5
  40. check_message_id_md5
  41. end
  42. def metadata_email_process_email_address
  43. # set sender
  44. email_address = ticket.group.email_address
  45. if !email_address
  46. raise "No email address found for group '#{ticket.group.fullname}' (#{ticket.group_id})"
  47. end
  48. # remember email address for background job
  49. preferences['email_address_id'] = email_address.id
  50. end
  51. def recipient_name(email_address)
  52. if created_by_id != 1
  53. case Setting.get('ticket_define_email_from')
  54. when 'AgentNameSystemAddressName'
  55. separator = Setting.get('ticket_define_email_from_separator')
  56. return "#{created_by.firstname} #{created_by.lastname} #{separator} #{email_address.name}"
  57. when 'AgentName'
  58. return "#{created_by.firstname} #{created_by.lastname}"
  59. end
  60. end
  61. email_address.name
  62. end
  63. def metadata_email_process_from
  64. email_address = ticket.group.email_address
  65. self.from = Channel::EmailBuild.recipient_line(recipient_name(email_address), email_address.email)
  66. end
  67. end