Implement postmark conversion from 'OriginalRecipient' to 'X-Original-To'

This commit is contained in:
Dimitri Jorge 2024-08-28 17:34:15 +01:00 committed by Rafael Mendonça França
parent 325d273f72
commit dfcbce85a3
No known key found for this signature in database
GPG Key ID: FC23B6D0F1EEE948
2 changed files with 24 additions and 1 deletions

View File

@ -49,7 +49,7 @@ module ActionMailbox
param_encoding :create, "RawEmail", Encoding::ASCII_8BIT
def create
ActionMailbox::InboundEmail.create_and_extract_message_id! params.require("RawEmail")
ActionMailbox::InboundEmail.create_and_extract_message_id! mail
rescue ActionController::ParameterMissing => error
logger.error <<~MESSAGE
#{error.message}
@ -59,5 +59,12 @@ module ActionMailbox
MESSAGE
head :unprocessable_entity
end
private
def mail
params.require("RawEmail").tap do |raw_email|
raw_email.prepend("X-Original-To: ", params.require("OriginalRecipient"), "\n") if params.key?("OriginalRecipient")
end
end
end
end

View File

@ -31,6 +31,22 @@ class ActionMailbox::Ingresses::Postmark::InboundEmailsControllerTest < ActionDi
assert_equal "05988AA6EC0D44318855A5E39E3B6F9E@jansterba.com", inbound_email.message_id
end
test "add X-Original-To to email from Postmark" do
assert_difference -> { ActionMailbox::InboundEmail.count }, +1 do
post rails_postmark_inbound_emails_url,
headers: { authorization: credentials }, params: {
RawEmail: file_fixture("../files/welcome.eml").read,
OriginalRecipient: "thisguy@domain.abcd",
}
end
assert_response :no_content
inbound_email = ActionMailbox::InboundEmail.last
mail = Mail.from_source(inbound_email.raw_email.download)
assert_equal "thisguy@domain.abcd", mail.header["X-Original-To"].decoded
end
test "rejecting when RawEmail param is missing" do
assert_no_difference -> { ActionMailbox::InboundEmail.count } do
post rails_postmark_inbound_emails_url,