change message gateway to save received email as attachment to the todo

This commit is contained in:
Reinier Balt 2014-07-03 23:24:29 +02:00
parent 76de5bf2b3
commit 4d7c453ca7
4 changed files with 51 additions and 2 deletions

View file

@ -1,3 +1,5 @@
require 'tempfile'
class MessageGateway < ActionMailer::Base
def receive(email)
@ -10,8 +12,26 @@ class MessageGateway < ActionMailer::Base
todo_builder = TodoFromRichMessage.new(user, context.id, todo_params[:description], todo_params[:notes])
todo = todo_builder.construct
todo.save!
Rails.logger.info "Saved email as todo for user #{user.login} in context #{context.name}"
saved = todo.save!
if saved
Rails.logger.info "Saved email as todo for user #{user.login} in context #{context.name}"
attachment = todo.attachments.build
tmp = Tempfile.new(['attachment', '.eml'], cr_newline: false)
tmp.write email.raw_source.gsub(/\r\n?/, "\n") # replace \r with \n
Rails.logger.info "Saved received email to #{tmp.path}"
attachment.file = tmp
tmp.close
saved = attachment.save!
tmp.unlink
if saved
Rails.logger.info "Saved email as todo for user #{user.login} in context #{context.name}"
end
end
todo
end