2014-02-05 16:14:57 +00:00
|
|
|
class Observer::Ticket::Article::CommunicateEmail::BackgroundJob
|
|
|
|
def initialize(id)
|
|
|
|
@article_id = id
|
|
|
|
end
|
2015-05-07 10:27:12 +00:00
|
|
|
|
2014-02-05 16:14:57 +00:00
|
|
|
def perform
|
2015-12-11 14:11:34 +00:00
|
|
|
record = Ticket::Article.find(@article_id)
|
2014-02-05 16:14:57 +00:00
|
|
|
|
|
|
|
# build subject
|
2016-05-23 17:56:39 +00:00
|
|
|
ticket = Ticket.lookup(id: record.ticket_id)
|
|
|
|
article_count = Ticket::Article.where(ticket_id: ticket.id).count
|
|
|
|
subject = if article_count > 1
|
|
|
|
ticket.subject_build(record.subject, true)
|
|
|
|
else
|
|
|
|
ticket.subject_build(record.subject)
|
|
|
|
end
|
2014-02-05 16:14:57 +00:00
|
|
|
|
2016-06-25 06:54:25 +00:00
|
|
|
# set retry count
|
|
|
|
if !record.preferences['delivery_retry']
|
|
|
|
record.preferences['delivery_retry'] = 0
|
|
|
|
end
|
|
|
|
record.preferences['delivery_retry'] += 1
|
|
|
|
|
2014-02-05 16:14:57 +00:00
|
|
|
# send email
|
2015-08-28 00:53:14 +00:00
|
|
|
if !ticket.group.email_address_id
|
2016-12-02 11:24:00 +00:00
|
|
|
log_error(record, "No email address defined for group id '#{ticket.group.id}'!")
|
2015-08-28 00:53:14 +00:00
|
|
|
elsif !ticket.group.email_address.channel_id
|
2016-12-02 11:24:00 +00:00
|
|
|
log_error(record, "No channel defined for email_address id '#{ticket.group.email_address_id}'!")
|
2015-08-28 00:53:14 +00:00
|
|
|
end
|
2015-12-11 14:11:34 +00:00
|
|
|
|
2015-08-28 00:53:14 +00:00
|
|
|
channel = ticket.group.email_address.channel
|
|
|
|
|
2016-05-03 00:36:44 +00:00
|
|
|
notification = false
|
|
|
|
sender = Ticket::Article::Sender.lookup(id: record.sender_id)
|
|
|
|
if sender['name'] == 'System'
|
|
|
|
notification = true
|
|
|
|
end
|
|
|
|
|
2015-08-28 00:53:14 +00:00
|
|
|
# get linked channel and send
|
2016-06-25 06:54:25 +00:00
|
|
|
begin
|
|
|
|
message = channel.deliver(
|
|
|
|
{
|
|
|
|
message_id: record.message_id,
|
|
|
|
in_reply_to: record.in_reply_to,
|
|
|
|
references: ticket.get_references([record.message_id]),
|
|
|
|
from: record.from,
|
|
|
|
to: record.to,
|
|
|
|
cc: record.cc,
|
|
|
|
subject: subject,
|
|
|
|
content_type: record.content_type,
|
|
|
|
body: record.body,
|
|
|
|
attachments: record.attachments
|
|
|
|
},
|
|
|
|
notification
|
|
|
|
)
|
|
|
|
rescue => e
|
|
|
|
log_error(record, e.message)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if !message
|
2016-06-27 11:24:48 +00:00
|
|
|
log_error(record, 'Unable to get sent email')
|
2016-06-25 06:54:25 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# set delivery status
|
|
|
|
record.preferences['delivery_status_message'] = nil
|
|
|
|
record.preferences['delivery_status'] = 'success'
|
|
|
|
record.preferences['delivery_status_date'] = Time.zone.now
|
2016-08-20 19:29:22 +00:00
|
|
|
record.save!
|
2014-02-05 16:14:57 +00:00
|
|
|
|
|
|
|
# store mail plain
|
|
|
|
Store.add(
|
2015-04-27 13:42:53 +00:00
|
|
|
object: 'Ticket::Article::Mail',
|
|
|
|
o_id: record.id,
|
|
|
|
data: message.to_s,
|
|
|
|
filename: "ticket-#{ticket.number}-#{record.id}.eml",
|
|
|
|
preferences: {},
|
|
|
|
created_by_id: record.created_by_id,
|
2014-02-05 16:14:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# add history record
|
|
|
|
recipient_list = ''
|
|
|
|
[:to, :cc].each { |key|
|
2015-05-07 09:04:40 +00:00
|
|
|
|
|
|
|
next if !record[key]
|
|
|
|
next if record[key] == ''
|
|
|
|
|
|
|
|
if recipient_list != ''
|
|
|
|
recipient_list += ','
|
2014-02-05 16:14:57 +00:00
|
|
|
end
|
2015-05-07 09:04:40 +00:00
|
|
|
recipient_list += record[key]
|
2014-02-05 16:14:57 +00:00
|
|
|
}
|
2015-04-30 15:25:04 +00:00
|
|
|
|
2016-08-20 19:29:22 +00:00
|
|
|
Rails.logger.info "Send email to: '#{recipient_list}' (from #{record.from})"
|
|
|
|
|
2015-04-30 15:25:04 +00:00
|
|
|
return if recipient_list == ''
|
|
|
|
|
|
|
|
History.add(
|
|
|
|
o_id: record.id,
|
|
|
|
history_type: 'email',
|
|
|
|
history_object: 'Ticket::Article',
|
|
|
|
related_o_id: ticket.id,
|
|
|
|
related_history_object: 'Ticket',
|
|
|
|
value_from: record.subject,
|
|
|
|
value_to: recipient_list,
|
|
|
|
created_by_id: record.created_by_id,
|
|
|
|
)
|
2014-02-05 16:14:57 +00:00
|
|
|
end
|
2016-06-25 06:54:25 +00:00
|
|
|
|
|
|
|
def log_error(local_record, message)
|
|
|
|
local_record.preferences['delivery_status'] = 'fail'
|
|
|
|
local_record.preferences['delivery_status_message'] = message
|
|
|
|
local_record.preferences['delivery_status_date'] = Time.zone.now
|
|
|
|
local_record.save
|
|
|
|
Rails.logger.error message
|
|
|
|
|
|
|
|
if local_record.preferences['delivery_retry'] > 3
|
2016-06-27 11:24:48 +00:00
|
|
|
|
|
|
|
recipient_list = ''
|
|
|
|
[:to, :cc].each { |key|
|
|
|
|
|
|
|
|
next if !local_record[key]
|
|
|
|
next if local_record[key] == ''
|
|
|
|
|
|
|
|
if recipient_list != ''
|
|
|
|
recipient_list += ','
|
|
|
|
end
|
|
|
|
recipient_list += local_record[key]
|
|
|
|
}
|
|
|
|
|
2016-06-25 06:54:25 +00:00
|
|
|
Ticket::Article.create(
|
|
|
|
ticket_id: local_record.ticket_id,
|
|
|
|
content_type: 'text/plain',
|
2016-06-27 12:09:34 +00:00
|
|
|
body: "Unable to send email to '#{recipient_list}': #{message}",
|
2016-06-25 06:54:25 +00:00
|
|
|
internal: true,
|
|
|
|
sender: Ticket::Article::Sender.find_by(name: 'System'),
|
|
|
|
type: Ticket::Article::Type.find_by(name: 'note'),
|
2016-06-27 11:24:48 +00:00
|
|
|
preferences: {
|
|
|
|
delivery_article_id_related: local_record.id,
|
|
|
|
delivery_message: true,
|
|
|
|
},
|
2016-06-25 06:54:25 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
raise message
|
|
|
|
end
|
|
|
|
|
|
|
|
def max_attempts
|
|
|
|
4
|
|
|
|
end
|
|
|
|
|
|
|
|
def reschedule_at(current_time, attempts)
|
|
|
|
if Rails.env.production?
|
|
|
|
return current_time + attempts * 20.seconds
|
|
|
|
end
|
|
|
|
current_time + 5.seconds
|
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|