2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-12-24 13:55:43 +00:00
|
|
|
class Observer::Ticket::Article::FillupFromEmail < ActiveRecord::Observer
|
|
|
|
observe 'ticket::_article'
|
|
|
|
|
|
|
|
def before_create(record)
|
|
|
|
|
|
|
|
# return if we run import mode
|
|
|
|
return if Setting.get('import_mode')
|
|
|
|
|
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)
|
|
|
|
return if ApplicationHandleInfo.current.split('.')[1] == 'postmaster'
|
|
|
|
|
2012-12-24 13:55:43 +00:00
|
|
|
# if sender is customer, do not change anything
|
2016-08-23 18:46:04 +00:00
|
|
|
return if !record.sender_id
|
2016-03-08 06:32:58 +00:00
|
|
|
sender = Ticket::Article::Sender.lookup(id: record.sender_id)
|
2015-05-07 10:11:45 +00:00
|
|
|
return if sender.nil?
|
2012-12-24 13:55:43 +00:00
|
|
|
return if sender['name'] == 'Customer'
|
|
|
|
|
|
|
|
# set email attributes
|
2016-08-23 18:46:04 +00:00
|
|
|
return if !record.type_id
|
2016-03-08 06:32:58 +00:00
|
|
|
type = Ticket::Article::Type.lookup(id: record.type_id)
|
2012-12-24 13:55:43 +00:00
|
|
|
return if type['name'] != 'email'
|
|
|
|
|
|
|
|
# set subject if empty
|
2016-03-08 06:32:58 +00:00
|
|
|
ticket = Ticket.lookup(id: record.ticket_id)
|
2012-12-24 13:55:43 +00:00
|
|
|
if !record.subject || record.subject == ''
|
|
|
|
record.subject = ticket.title
|
|
|
|
end
|
|
|
|
|
|
|
|
# clean subject
|
2016-03-08 06:32:58 +00:00
|
|
|
record.subject = ticket.subject_clean(record.subject)
|
2012-12-24 13:55:43 +00:00
|
|
|
|
2015-08-31 23:07:07 +00:00
|
|
|
# generate message id, force it in prodution, in test allow to set it for testing reasons
|
|
|
|
if !record.message_id || Rails.env.production?
|
|
|
|
fqdn = Setting.get('fqdn')
|
2016-08-24 18:29:56 +00:00
|
|
|
record.message_id = "<#{DateTime.current.to_s(:number)}.#{record.ticket_id}.#{rand(999_999)}@#{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
|
|
|
|
record.check_message_id_md5
|
|
|
|
|
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
|
2016-03-01 14:26:46 +00:00
|
|
|
raise "No email address found for group '#{ticket.group.name}'"
|
2013-06-19 12:22:13 +00:00
|
|
|
end
|
2017-03-24 16:43:09 +00:00
|
|
|
|
|
|
|
# remember email address for background job
|
|
|
|
record.preferences['email_address_id'] = email_address.id
|
|
|
|
|
|
|
|
# fill from
|
2016-05-03 00:36:44 +00:00
|
|
|
if record.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')
|
2016-11-25 12:43:28 +00:00
|
|
|
sender = User.find(record.created_by_id)
|
2017-01-16 14:12:06 +00:00
|
|
|
realname = "#{sender.firstname} #{sender.lastname} #{separator} #{email_address.realname}"
|
2016-11-25 12:43:28 +00:00
|
|
|
record.from = Channel::EmailBuild.recipient_line(realname, email_address.email)
|
2012-12-24 13:55:43 +00:00
|
|
|
else
|
2016-11-25 12:43:28 +00:00
|
|
|
record.from = Channel::EmailBuild.recipient_line(email_address.realname, email_address.email)
|
2012-12-24 13:55:43 +00:00
|
|
|
end
|
|
|
|
end
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|