mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Extract and associate message_id with newly created inbound emails
This commit is contained in:
parent
8eb239bd1a
commit
96b6e7ce66
7 changed files with 36 additions and 6 deletions
|
@ -4,7 +4,7 @@ class ActionMailroom::InboundEmailsController < ActionController::Base
|
|||
before_action :require_rfc822_message
|
||||
|
||||
def create
|
||||
ActionMailroom::InboundEmail.create!(raw_email: params[:message])
|
||||
ActionMailroom::InboundEmail.create_from_raw_email!(params[:message])
|
||||
head :created
|
||||
end
|
||||
|
||||
|
|
|
@ -9,7 +9,22 @@ class ActionMailroom::InboundEmail < ActiveRecord::Base
|
|||
has_one_attached :raw_email
|
||||
enum status: %i[ pending processing delivered failed bounced ]
|
||||
|
||||
class << self
|
||||
def create_from_raw_email!(raw_email, **options)
|
||||
create! raw_email: raw_email, message_id: extract_message_id(raw_email), **options
|
||||
end
|
||||
|
||||
def mail_from_raw_content(raw_email_content)
|
||||
Mail.new(Mail::Utilities.binary_unsafe_to_crlf(raw_email_content.to_s))
|
||||
end
|
||||
|
||||
private
|
||||
def extract_message_id(raw_email)
|
||||
mail_from_raw_content(raw_email.read).message_id
|
||||
end
|
||||
end
|
||||
|
||||
def mail
|
||||
@mail ||= Mail.new(Mail::Utilities.binary_unsafe_to_crlf(raw_email.download))
|
||||
@mail ||= self.class.mail_from_raw_content(raw_email.download)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,6 +2,7 @@ class CreateActionMailroomTables < ActiveRecord::Migration[5.2]
|
|||
def change
|
||||
create_table :action_mailroom_inbound_emails do |t|
|
||||
t.integer :status, default: 0, null: false
|
||||
t.string :message_id
|
||||
|
||||
t.datetime :created_at, precision: 6
|
||||
t.datetime :updated_at, precision: 6
|
||||
|
|
|
@ -5,16 +5,18 @@ module ActionMailroom
|
|||
# Create an InboundEmail record using an eml fixture in the format of message/rfc822
|
||||
# referenced with +fixture_name+ located in +test/fixtures/files/fixture_name+.
|
||||
def create_inbound_email_from_fixture(fixture_name, status: :processing)
|
||||
create_inbound_email file_fixture(fixture_name).open, filename: fixture_name, status: status
|
||||
create_inbound_email file_fixture(fixture_name), filename: fixture_name, status: status
|
||||
end
|
||||
|
||||
def create_inbound_email_from_mail(status: :processing, **mail_options)
|
||||
create_inbound_email(StringIO.new(Mail.new(mail_options).to_s), status: status)
|
||||
raw_email = Tempfile.new.tap { |raw_email| raw_email.write Mail.new(mail_options).to_s }
|
||||
create_inbound_email(raw_email, status: status)
|
||||
end
|
||||
|
||||
def create_inbound_email(io, filename: 'mail.eml', status: :processing)
|
||||
ActionMailroom::InboundEmail.create! status: status, raw_email:
|
||||
ActiveStorage::Blob.create_after_upload!(io: io, filename: filename, content_type: 'message/rfc822')
|
||||
ActionMailroom::InboundEmail.create_from_raw_email! \
|
||||
ActionDispatch::Http::UploadedFile.new(tempfile: io, filename: filename, type: 'message/rfc822'),
|
||||
status: status
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,6 +2,7 @@ class CreateActionMailroomTables < ActiveRecord::Migration[5.2]
|
|||
def change
|
||||
create_table :action_mailroom_inbound_emails do |t|
|
||||
t.integer :status, default: 0, null: false
|
||||
t.string :message_id
|
||||
|
||||
t.datetime :created_at, precision: 6
|
||||
t.datetime :updated_at, precision: 6
|
||||
|
|
|
@ -14,6 +14,7 @@ ActiveRecord::Schema.define(version: 2018_02_12_164506) do
|
|||
|
||||
create_table "action_mailroom_inbound_emails", force: :cascade do |t|
|
||||
t.integer "status", default: 0, null: false
|
||||
t.string "message_id"
|
||||
t.datetime "created_at", precision: 6
|
||||
t.datetime "updated_at", precision: 6
|
||||
end
|
||||
|
|
10
test/unit/inbound_email_test.rb
Normal file
10
test/unit/inbound_email_test.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
require_relative '../test_helper'
|
||||
|
||||
module ActionMailroom
|
||||
class InboundEmailTest < ActiveSupport::TestCase
|
||||
test "message id is extracted from raw email" do
|
||||
inbound_email = create_inbound_email_from_fixture("welcome.eml")
|
||||
assert_equal "0CB459E0-0336-41DA-BC88-E6E28C697DDB@37signals.com", inbound_email.message_id
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue