mailer.rb 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class NotificationFactory::Mailer
  3. =begin
  4. get notification settings for user and notification type
  5. result = NotificationFactory::Mailer.notification_settings(user, ticket, type)
  6. type: create | update | reminder_reached | escalation (escalation_warning)
  7. returns
  8. {
  9. user: user,
  10. channels: {
  11. online: true,
  12. email: true,
  13. },
  14. }
  15. =end
  16. def self.notification_settings(user, ticket, type)
  17. # map types if needed
  18. map = {
  19. 'escalation_warning' => 'escalation'
  20. }
  21. type = type.split('.').first # pick parent type of a subtype. Eg. update vs update.merged_into
  22. if map[type]
  23. type = map[type]
  24. end
  25. # this cache will optimize the preference catch performance
  26. # because of the yaml deserialization its pretty slow
  27. # on many tickets you we cache it.
  28. user_preferences = Rails.cache.read("NotificationFactory::Mailer.notification_settings::#{user.id}")
  29. if user_preferences.blank?
  30. user_preferences = user.preferences
  31. Rails.cache.write("NotificationFactory::Mailer.notification_settings::#{user.id}", user_preferences, expires_in: 20.seconds)
  32. end
  33. return if !user_preferences
  34. return if !user_preferences['notification_config']
  35. matrix = user_preferences['notification_config']['matrix']
  36. return if !matrix
  37. case ticket.owner_id
  38. when 1
  39. owned_by_nobody = true
  40. when user.id
  41. owned_by_me = true
  42. else
  43. # check the replacement chain of max 10
  44. # if the current user is in it
  45. check_for = ticket.owner
  46. 10.times do
  47. replacement = check_for.out_of_office_agent
  48. break if !replacement
  49. check_for = replacement
  50. next if replacement.id != user.id
  51. owned_by_me = true
  52. break
  53. end
  54. end
  55. # always trigger notifications for user if he is subscribed
  56. if !owned_by_me && ticket.mentions.exists?(user: user)
  57. subscribed = true
  58. end
  59. # check if group is in selected groups
  60. if !owned_by_me && !subscribed
  61. selected_group_ids = user_preferences['notification_config']['group_ids']
  62. if selected_group_ids.is_a?(Array)
  63. hit = nil
  64. if selected_group_ids.blank? || (selected_group_ids[0] == '-' && selected_group_ids.count == 1)
  65. hit = true
  66. else
  67. hit = false
  68. selected_group_ids.each do |selected_group_id|
  69. if selected_group_id.to_s == ticket.group_id.to_s
  70. hit = true
  71. break
  72. end
  73. end
  74. end
  75. return if !hit # no group access
  76. end
  77. end
  78. return if !matrix[type]
  79. data = matrix[type]
  80. return if !data
  81. return if !data['criteria']
  82. channels = data['channel']
  83. return if !channels
  84. if data['criteria']['owned_by_me'] && owned_by_me
  85. return {
  86. user: user,
  87. channels: channels
  88. }
  89. end
  90. if data['criteria']['owned_by_nobody'] && owned_by_nobody
  91. return {
  92. user: user,
  93. channels: channels
  94. }
  95. end
  96. if data['criteria']['subscribed'] && subscribed
  97. return {
  98. user: user,
  99. channels: channels
  100. }
  101. end
  102. # 'no' actually means 'no criteria' or 'for all tickets'.
  103. return if !data['criteria']['no']
  104. {
  105. user: user,
  106. channels: channels
  107. }
  108. end
  109. =begin
  110. success = NotificationFactory::Mailer.deliver(
  111. recipient: User.find(123),
  112. subject: 'some subject',
  113. body: 'some body',
  114. content_type: '', # optional, e. g. 'text/html'
  115. message_id: '<some_message_id@fqdn>', # optional
  116. references: ['message-id123', 'message-id456'], # optional
  117. attachments: [attachments...], # optional
  118. )
  119. =end
  120. def self.deliver(data)
  121. raise Exceptions::UnprocessableEntity, "Unable to send mail to user with id #{data[:recipient][:id]} because there is no email available." if data[:recipient][:email].blank?
  122. sender = Setting.get('notification_sender')
  123. Rails.logger.debug { "Send notification to: #{data[:recipient][:email]} (from:#{sender}/subject:#{data[:subject]})" }
  124. content_type = 'text/plain'
  125. if data[:content_type]
  126. content_type = data[:content_type]
  127. end
  128. # get active Email::Outbound Channel and send
  129. channel = Channel.find_by(area: 'Email::Notification', active: true)
  130. if channel.blank?
  131. Rails.logger.info "Can't find an active 'Email::Notification' channel. Canceling notification sending."
  132. return false
  133. end
  134. channel.deliver(
  135. {
  136. # in_reply_to: in_reply_to,
  137. from: sender,
  138. to: data[:recipient][:email],
  139. subject: data[:subject],
  140. message_id: data[:message_id],
  141. references: data[:references],
  142. body: data[:body],
  143. content_type: content_type,
  144. attachments: data[:attachments],
  145. },
  146. true
  147. )
  148. end
  149. =begin
  150. NotificationFactory::Mailer.notification(
  151. template: 'password_reset',
  152. user: User.find(2),
  153. objects: {
  154. recipient: User.find(2),
  155. },
  156. main_object: ticket.find(123), # optional
  157. message_id: '<some_message_id@fqdn>', # optional
  158. references: ['message-id123', 'message-id456'], # optional
  159. standalone: true, # default: false - will send header & footer
  160. attachments: [attachments...], # optional
  161. )
  162. =end
  163. def self.notification(data)
  164. # get subject
  165. result = NotificationFactory::Mailer.template(
  166. template: data[:template],
  167. locale: data[:user][:preferences][:locale],
  168. objects: data[:objects],
  169. standalone: data[:standalone],
  170. )
  171. # rebuild subject
  172. if data[:main_object].respond_to?(:subject_build)
  173. result[:subject] = data[:main_object].subject_build(result[:subject])
  174. end
  175. # prepare scaling of images
  176. if result[:body]
  177. result[:body] = HtmlSanitizer.adjust_inline_image_size(result[:body])
  178. result[:body] = HtmlSanitizer.dynamic_image_size(result[:body])
  179. end
  180. NotificationFactory::Mailer.deliver(
  181. recipient: data[:user],
  182. subject: result[:subject],
  183. body: result[:body],
  184. content_type: 'text/html',
  185. message_id: data[:message_id],
  186. references: data[:references],
  187. attachments: data[:attachments],
  188. )
  189. end
  190. =begin
  191. get count of already sent notifications
  192. count = NotificationFactory::Mailer.already_sent?(ticket, recipient_user, type)
  193. retunes
  194. 8
  195. =end
  196. def self.already_sent?(ticket, recipient, type)
  197. result = ticket.history_get
  198. count = 0
  199. result.each do |item|
  200. next if item['type'] != 'notification'
  201. next if item['object'] != 'Ticket'
  202. next if !item['value_to'].match?(%r{#{recipient.email}}i)
  203. next if !item['value_to'].match?(%r{#{type}}i)
  204. count += 1
  205. end
  206. count
  207. end
  208. =begin
  209. result = NotificationFactory::Mailer.template(
  210. template: 'password_reset',
  211. locale: 'en-us',
  212. timezone: 'America/Santiago',
  213. objects: {
  214. recipient: User.find(2),
  215. },
  216. )
  217. result = NotificationFactory::Mailer.template(
  218. templateInline: "Invitation to \#{config.product_name} at \#{config.fqdn}",
  219. locale: 'en-us',
  220. timezone: 'America/Santiago',
  221. objects: {
  222. recipient: User.find(2),
  223. },
  224. quote: true, # html quoting
  225. )
  226. only raw subject/body
  227. result = NotificationFactory::Mailer.template(
  228. template: 'password_reset',
  229. locale: 'en-us',
  230. timezone: 'America/Santiago',
  231. objects: {
  232. recipient: User.find(2),
  233. },
  234. raw: true, # will not add application template
  235. standalone: true, # default: false - will send header & footer
  236. )
  237. returns
  238. {
  239. subject: 'some subject',
  240. body: 'some body',
  241. }
  242. =end
  243. def self.template(data)
  244. if data[:templateInline]
  245. return NotificationFactory::Renderer.new(
  246. objects: data[:objects],
  247. locale: data[:locale],
  248. timezone: data[:timezone],
  249. template: data[:templateInline],
  250. escape: data[:quote]
  251. ).render
  252. end
  253. template = NotificationFactory.template_read(
  254. locale: data[:locale] || Locale.default,
  255. template: data[:template],
  256. format: data[:format] || 'html',
  257. type: 'mailer',
  258. )
  259. message_subject = NotificationFactory::Renderer.new(
  260. objects: data[:objects],
  261. locale: data[:locale],
  262. timezone: data[:timezone],
  263. template: template[:subject],
  264. escape: false,
  265. trusted: true,
  266. ).render
  267. # strip off the extra newline at the end of the subject to avoid =0A suffixes (see #2726)
  268. message_subject.chomp!
  269. message_body = NotificationFactory::Renderer.new(
  270. objects: data[:objects],
  271. locale: data[:locale],
  272. timezone: data[:timezone],
  273. template: template[:body],
  274. trusted: true,
  275. ).render
  276. if !data[:raw]
  277. application_template = NotificationFactory.application_template_read(
  278. format: 'html',
  279. type: 'mailer',
  280. )
  281. data[:objects][:message] = message_body
  282. data[:objects][:standalone] = data[:standalone]
  283. message_body = NotificationFactory::Renderer.new(
  284. objects: data[:objects],
  285. locale: data[:locale],
  286. timezone: data[:timezone],
  287. template: application_template,
  288. trusted: true,
  289. ).render
  290. end
  291. {
  292. subject: message_subject,
  293. body: message_body,
  294. }
  295. end
  296. end