Moved to background jobs for mail sending.
This commit is contained in:
parent
eb47a0c2c8
commit
eb0a4bd7a9
1 changed files with 56 additions and 47 deletions
|
@ -17,56 +17,65 @@ class Observer::Ticket::Article::CommunicateEmail < ActiveRecord::Observer
|
||||||
type = Ticket::Article::Type.lookup( :id => record.ticket_article_type_id )
|
type = Ticket::Article::Type.lookup( :id => record.ticket_article_type_id )
|
||||||
return if type['name'] != 'email'
|
return if type['name'] != 'email'
|
||||||
|
|
||||||
# build subject
|
Delayed::Job.enqueue( Observer::Ticket::Article::CommunicateEmail::Send.new( record.id )
|
||||||
ticket = Ticket.lookup( :id => record.ticket_id )
|
end
|
||||||
subject = ticket.subject_build( record.subject )
|
|
||||||
|
|
||||||
# send email
|
class Send < Struct.new( :id )
|
||||||
a = Channel::IMAP.new
|
def perform
|
||||||
message = a.send(
|
record = Ticket::Article.find( id )
|
||||||
{
|
|
||||||
:message_id => record.message_id,
|
|
||||||
:in_reply_to => record.in_reply_to,
|
|
||||||
:from => record.from,
|
|
||||||
:to => record.to,
|
|
||||||
:cc => record.cc,
|
|
||||||
:subject => subject,
|
|
||||||
:body => record.body,
|
|
||||||
:attachments => record.attachments
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
# store mail plain
|
# build subject
|
||||||
Store.add(
|
ticket = Ticket.lookup( :id => record.ticket_id )
|
||||||
:object => 'Ticket::Article::Mail',
|
subject = ticket.subject_build( record.subject )
|
||||||
:o_id => record.id,
|
|
||||||
:data => message.to_s,
|
|
||||||
:filename => "ticket-#{ticket.number}-#{record.id}.eml",
|
|
||||||
:preferences => {},
|
|
||||||
:created_by_id => record.created_by_id,
|
|
||||||
)
|
|
||||||
|
|
||||||
# add history record
|
# send email
|
||||||
recipient_list = ''
|
a = Channel::IMAP.new
|
||||||
[:to, :cc].each { |key|
|
message = a.send(
|
||||||
if record[key] && record[key] != ''
|
{
|
||||||
if recipient_list != ''
|
:message_id => record.message_id,
|
||||||
recipient_list += ','
|
:in_reply_to => record.in_reply_to,
|
||||||
end
|
:from => record.from,
|
||||||
recipient_list += record[key]
|
:to => record.to,
|
||||||
end
|
:cc => record.cc,
|
||||||
}
|
:subject => subject,
|
||||||
if recipient_list != ''
|
:body => record.body,
|
||||||
History.add(
|
:attachments => record.attachments
|
||||||
: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,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# store mail plain
|
||||||
|
Store.add(
|
||||||
|
: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,
|
||||||
|
)
|
||||||
|
|
||||||
|
# add history record
|
||||||
|
recipient_list = ''
|
||||||
|
[:to, :cc].each { |key|
|
||||||
|
if record[key] && record[key] != ''
|
||||||
|
if recipient_list != ''
|
||||||
|
recipient_list += ','
|
||||||
|
end
|
||||||
|
recipient_list += record[key]
|
||||||
|
end
|
||||||
|
}
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
end
|
Loading…
Reference in a new issue