ticket_article_communicate_email_job.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. class TicketArticleCommunicateEmailJob < ApplicationJob
  2. retry_on StandardError, attempts: 4, wait: lambda { |executions|
  3. executions * 25.seconds
  4. }
  5. def perform(article_id)
  6. record = Ticket::Article.find(article_id)
  7. # build subject
  8. ticket = Ticket.lookup(id: record.ticket_id)
  9. subject_prefix_mode = record.preferences[:subtype]
  10. subject = ticket.subject_build(record.subject, subject_prefix_mode)
  11. # set retry count
  12. record.preferences['delivery_retry'] ||= 0
  13. record.preferences['delivery_retry'] += 1
  14. # send email
  15. email_address = nil
  16. if record.preferences['email_address_id'].present?
  17. email_address = EmailAddress.find_by(id: record.preferences['email_address_id'])
  18. end
  19. # fallback for articles without email_address_id
  20. if !email_address
  21. if !ticket.group.email_address_id
  22. log_error(record, "No email address defined for group id '#{ticket.group.id}'!")
  23. elsif !ticket.group.email_address.channel_id
  24. log_error(record, "No channel defined for email_address id '#{ticket.group.email_address_id}'!")
  25. end
  26. email_address = ticket.group.email_address
  27. end
  28. # log if ref objects are missing
  29. if !email_address
  30. log_error(record, "No email address defined for group id '#{ticket.group_id}'!")
  31. end
  32. if !email_address.channel_id
  33. log_error(record, "No channel defined for email_address id '#{email_address.id}'!")
  34. end
  35. channel = email_address.channel
  36. notification = false
  37. sender = Ticket::Article::Sender.lookup(id: record.sender_id)
  38. if sender['name'] == 'System'
  39. notification = true
  40. end
  41. # get linked channel and send
  42. begin
  43. message = channel.deliver(
  44. {
  45. message_id: record.message_id,
  46. in_reply_to: record.in_reply_to,
  47. references: ticket.get_references([record.message_id]),
  48. from: record.from,
  49. to: record.to,
  50. cc: record.cc,
  51. subject: subject,
  52. content_type: record.content_type,
  53. body: record.body,
  54. attachments: record.attachments,
  55. security: record.preferences[:security],
  56. },
  57. notification
  58. )
  59. rescue => e
  60. log_error(record, e.message, channel)
  61. return
  62. end
  63. if !message
  64. log_error(record, 'Unable to get sent email', channel)
  65. return
  66. end
  67. # set delivery status
  68. record.preferences['delivery_channel_id'] = channel.id
  69. record.preferences['delivery_status_message'] = nil
  70. record.preferences['delivery_status'] = 'success'
  71. record.preferences['delivery_status_date'] = Time.zone.now
  72. record.save!
  73. # store mail plain
  74. record.save_as_raw(message.to_s)
  75. # add history record
  76. recipient_list = ''
  77. %i[to cc].each do |key|
  78. next if !record[key]
  79. next if record[key] == ''
  80. if recipient_list != ''
  81. recipient_list += ','
  82. end
  83. recipient_list += record[key]
  84. end
  85. Rails.logger.info "Send email to: '#{recipient_list}' (from #{record.from})"
  86. return if recipient_list == ''
  87. History.add(
  88. o_id: record.id,
  89. history_type: 'email',
  90. history_object: 'Ticket::Article',
  91. related_o_id: ticket.id,
  92. related_history_object: 'Ticket',
  93. value_from: record.subject,
  94. value_to: recipient_list,
  95. created_by_id: record.created_by_id,
  96. )
  97. end
  98. def log_error(local_record, message, channel = nil)
  99. if channel
  100. local_record.preferences['delivery_channel_id'] = channel.id
  101. end
  102. local_record.preferences['delivery_status'] = 'fail'
  103. local_record.preferences['delivery_status_message'] = message.encode!('UTF-8', 'UTF-8', invalid: :replace, replace: '?')
  104. local_record.preferences['delivery_status_date'] = Time.zone.now
  105. local_record.save!
  106. Rails.logger.error message
  107. if local_record.preferences['delivery_retry'] > 3
  108. recipient_list = ''
  109. %i[to cc].each do |key|
  110. next if !local_record[key]
  111. next if local_record[key] == ''
  112. if recipient_list != ''
  113. recipient_list += ','
  114. end
  115. recipient_list += local_record[key]
  116. end
  117. # reopen ticket and notify agent
  118. Observer::Transaction.reset
  119. UserInfo.current_user_id = 1
  120. Ticket::Article.create!(
  121. ticket_id: local_record.ticket_id,
  122. content_type: 'text/plain',
  123. body: "Unable to send email to '#{recipient_list}': #{message}",
  124. internal: true,
  125. sender: Ticket::Article::Sender.find_by(name: 'System'),
  126. type: Ticket::Article::Type.find_by(name: 'note'),
  127. preferences: {
  128. delivery_article_id_related: local_record.id,
  129. delivery_message: true,
  130. notification: true,
  131. },
  132. )
  133. ticket = Ticket.find(local_record.ticket_id)
  134. ticket.state = Ticket::State.find_by(default_follow_up: true)
  135. ticket.save!
  136. Observer::Transaction.commit
  137. UserInfo.current_user_id = nil
  138. end
  139. raise message
  140. end
  141. end