diff --git a/app/models/observer/ticket/article/communicate_email.rb b/app/models/observer/ticket/article/communicate_email.rb index 83bdc0688..9a83d7c4c 100644 --- a/app/models/observer/ticket/article/communicate_email.rb +++ b/app/models/observer/ticket/article/communicate_email.rb @@ -17,56 +17,65 @@ class Observer::Ticket::Article::CommunicateEmail < ActiveRecord::Observer type = Ticket::Article::Type.lookup( :id => record.ticket_article_type_id ) return if type['name'] != 'email' - # build subject - ticket = Ticket.lookup( :id => record.ticket_id ) - subject = ticket.subject_build( record.subject ) + Delayed::Job.enqueue( Observer::Ticket::Article::CommunicateEmail::Send.new( record.id ) + end - # send email - a = Channel::IMAP.new - message = a.send( - { - :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 - } - ) + class Send < Struct.new( :id ) + def perform + record = Ticket::Article.find( 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, - ) + # build subject + ticket = Ticket.lookup( :id => record.ticket_id ) + subject = ticket.subject_build( record.subject ) - # 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, + # send email + a = Channel::IMAP.new + message = a.send( + { + :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 + 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 \ No newline at end of file