2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2021-03-01 08:18:40 +00:00
|
|
|
# Adds certain (missing) meta data when creating email articles.
|
|
|
|
module Ticket::Article::AddsMetadataEmail
|
|
|
|
extend ActiveSupport::Concern
|
2012-12-24 13:55:43 +00:00
|
|
|
|
2021-03-01 08:18:40 +00:00
|
|
|
included do
|
|
|
|
before_create :ticket_article_add_metadata_email
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def ticket_article_add_metadata_email
|
2012-12-24 13:55:43 +00:00
|
|
|
|
|
|
|
# return if we run import mode
|
2017-06-16 22:53:20 +00:00
|
|
|
return true if Setting.get('import_mode')
|
2012-12-24 13:55:43 +00:00
|
|
|
|
2016-08-20 19:29:22 +00:00
|
|
|
# only do fill of email from if article got created via application_server (e. g. not
|
|
|
|
# if article and sender type is set via *.postmaster)
|
2017-11-23 08:09:44 +00:00
|
|
|
return if ApplicationHandleInfo.postmaster?
|
2016-08-20 19:29:22 +00:00
|
|
|
|
2012-12-24 13:55:43 +00:00
|
|
|
# if sender is customer, do not change anything
|
2021-03-01 08:18:40 +00:00
|
|
|
return true if !sender_id
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2021-03-01 08:18:40 +00:00
|
|
|
sender = Ticket::Article::Sender.lookup(id: sender_id)
|
2017-06-16 22:53:20 +00:00
|
|
|
return true if sender.nil?
|
2018-10-16 08:45:15 +00:00
|
|
|
return true if sender.name == 'Customer'
|
2012-12-24 13:55:43 +00:00
|
|
|
|
|
|
|
# set email attributes
|
2021-03-01 08:18:40 +00:00
|
|
|
return true if !type_id
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2021-03-01 08:18:40 +00:00
|
|
|
type = Ticket::Article::Type.lookup(id: type_id)
|
2018-10-16 08:45:15 +00:00
|
|
|
return true if type.nil?
|
|
|
|
return true if type.name != 'email'
|
2012-12-24 13:55:43 +00:00
|
|
|
|
|
|
|
# set subject if empty
|
2021-03-01 08:18:40 +00:00
|
|
|
ticket = self.ticket
|
|
|
|
if !subject || subject == ''
|
|
|
|
self.subject = ticket.title
|
2012-12-24 13:55:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# clean subject
|
2021-03-01 08:18:40 +00:00
|
|
|
self.subject = ticket.subject_clean(subject)
|
2012-12-24 13:55:43 +00:00
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
# generate message id, force it in production, in test allow to set it for testing reasons
|
2021-03-01 08:18:40 +00:00
|
|
|
if !message_id || Rails.env.production?
|
2015-08-31 23:07:07 +00:00
|
|
|
fqdn = Setting.get('fqdn')
|
2021-09-20 10:47:05 +00:00
|
|
|
self.message_id = "<#{DateTime.current.to_s(:number)}.#{ticket_id}.#{SecureRandom.uuid}@#{fqdn}>"
|
2015-08-31 23:07:07 +00:00
|
|
|
end
|
2012-12-24 13:55:43 +00:00
|
|
|
|
2015-08-30 20:15:23 +00:00
|
|
|
# generate message_id_md5
|
2021-03-01 08:18:40 +00:00
|
|
|
check_message_id_md5
|
2015-08-30 20:15:23 +00:00
|
|
|
|
2012-12-24 13:55:43 +00:00
|
|
|
# set sender
|
|
|
|
email_address = ticket.group.email_address
|
2013-06-19 12:22:13 +00:00
|
|
|
if !email_address
|
2018-07-13 17:50:10 +00:00
|
|
|
raise "No email address found for group '#{ticket.group.name}' (#{ticket.group_id})"
|
2013-06-19 12:22:13 +00:00
|
|
|
end
|
2017-03-24 16:43:09 +00:00
|
|
|
|
|
|
|
# remember email address for background job
|
2021-03-01 08:18:40 +00:00
|
|
|
preferences['email_address_id'] = email_address.id
|
2017-03-24 16:43:09 +00:00
|
|
|
|
|
|
|
# fill from
|
2021-03-01 08:18:40 +00:00
|
|
|
if created_by_id != 1 && Setting.get('ticket_define_email_from') == 'AgentNameSystemAddressName'
|
2017-01-16 14:12:06 +00:00
|
|
|
separator = Setting.get('ticket_define_email_from_separator')
|
2021-03-01 08:18:40 +00:00
|
|
|
sender = User.find(created_by_id)
|
2017-01-16 14:12:06 +00:00
|
|
|
realname = "#{sender.firstname} #{sender.lastname} #{separator} #{email_address.realname}"
|
2021-03-01 08:18:40 +00:00
|
|
|
self.from = Channel::EmailBuild.recipient_line(realname, email_address.email)
|
2020-12-01 10:34:04 +00:00
|
|
|
elsif Setting.get('ticket_define_email_from') == 'AgentName'
|
2021-03-01 08:18:40 +00:00
|
|
|
sender = User.find(created_by_id)
|
2020-12-01 10:34:04 +00:00
|
|
|
realname = "#{sender.firstname} #{sender.lastname}"
|
2022-06-16 14:15:00 +00:00
|
|
|
|
|
|
|
# avoid "-" as realname, see https://github.com/zammad/zammad/issues/3890
|
|
|
|
realname = email_address.realname if sender.id == 1
|
|
|
|
|
2021-03-01 08:18:40 +00:00
|
|
|
self.from = Channel::EmailBuild.recipient_line(realname, email_address.email)
|
2012-12-24 13:55:43 +00:00
|
|
|
else
|
2021-03-01 08:18:40 +00:00
|
|
|
self.from = Channel::EmailBuild.recipient_line(email_address.realname, email_address.email)
|
2012-12-24 13:55:43 +00:00
|
|
|
end
|
2017-06-16 22:53:20 +00:00
|
|
|
true
|
2012-12-24 13:55:43 +00:00
|
|
|
end
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|