Skip to content

Commit

Permalink
Sendgrid: prepend X-Original-To header with envelope recipients
Browse files Browse the repository at this point in the history
Sendgrid, like Mailgun, only passes BCC recipients as a parameter in the original JSON payload.

This PR adds code to prepend the recipients from the Sendgrid payload to the raw_email under the X-Original-To header.

References #38738.
  • Loading branch information
mhssmnn committed Jul 21, 2020
1 parent b949e69 commit 71c8a89
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
4 changes: 4 additions & 0 deletions actionmailbox/CHANGELOG.md
@@ -1,3 +1,7 @@
* Sendgrid ingress now passes through the envelope recipient as `X-Original-To`.

*Mark Haussmann*

* Update Mandrill inbound email route to respond appropriately to HEAD requests for URL health checks from Mandrill.

*Bill Cromie*
Expand Down
Expand Up @@ -48,7 +48,21 @@ class Ingresses::Sendgrid::InboundEmailsController < ActionMailbox::BaseControll
before_action :authenticate_by_password

def create
ActionMailbox::InboundEmail.create_and_extract_message_id! params.require(:email)
ActionMailbox::InboundEmail.create_and_extract_message_id! mail
rescue JSON::ParserError => error
logger.error error.message
head :unprocessable_entity
end

private
def mail
params.require(:email).tap do |raw_email|
envelope["to"].each { |to| raw_email.prepend("X-Original-To: ", to, "\n") } if params.key?(:envelope)
end
end

def envelope
JSON.parse(params.require(:envelope))
end
end
end
Expand Up @@ -18,6 +18,22 @@ class ActionMailbox::Ingresses::Sendgrid::InboundEmailsControllerTest < ActionDi
assert_equal "0CB459E0-0336-41DA-BC88-E6E28C697DDB@37signals.com", inbound_email.message_id
end

test "add X-Original-To to email from Sendgrid" do
assert_difference -> { ActionMailbox::InboundEmail.count }, +1 do
post rails_sendgrid_inbound_emails_url,
headers: { authorization: credentials }, params: {
email: file_fixture("../files/welcome.eml").read,
envelope: "{\"to\":[\"replies@example.com\"],\"from\":\"jason@37signals.com\"}",
}
end

assert_response :no_content

inbound_email = ActionMailbox::InboundEmail.last
mail = Mail.from_source(inbound_email.raw_email.download)
assert_equal "replies@example.com", mail.header["X-Original-To"].decoded
end

test "rejecting an unauthorized inbound email from Sendgrid" do
assert_no_difference -> { ActionMailbox::InboundEmail.count } do
post rails_sendgrid_inbound_emails_url, params: { email: file_fixture("../files/welcome.eml").read }
Expand Down

0 comments on commit 71c8a89

Please sign in to comment.