FIX: don't overwrite custom email headers when using mandrill/sparkpost

This commit is contained in:
Régis Hanol 2016-10-30 11:38:55 +01:00
parent 6aacc655d1
commit f8caae0be7
2 changed files with 30 additions and 2 deletions

View File

@ -138,9 +138,9 @@ module Email
when /\.mailjet\.com/
@message.header['X-MJ-CustomID'] = @message.message_id
when "smtp.mandrillapp.com"
@message.header['X-MC-Metadata'] = { message_id: @message.message_id }.to_json
merge_json_x_header('X-MC-Metadata', { message_id: @message.message_id })
when "smtp.sparkpostmail.com"
@message.header['X-MSYS-API'] = { metadata: { message_id: @message.message_id } }.to_json
merge_json_x_header('X-MSYS-API', { metadata: { message_id: @message.message_id } })
end
# Suppress images from short emails
@ -202,5 +202,13 @@ module Email
)
end
def merge_json_x_header(name, value)
mc_metadata = JSON.parse(@message.header[name].to_s) rescue nil
mc_metadata ||= {}
mc_metadata.merge!(value)
@message.header[name] = nil
@message.header[name] = mc_metadata.to_json
end
end
end

View File

@ -139,6 +139,26 @@ describe Email::Sender do
Then { expect(message.header['X-Discourse-Reply-Key']).not_to be_present }
end
context "merges custom mandrill header" do
before do
ActionMailer::Base.smtp_settings[:address] = "smtp.mandrillapp.com"
message.header['X-MC-Metadata'] = { foo: "bar" }.to_json
end
When { email_sender.send }
Then { expect(message.header['X-MC-Metadata'].to_s).to match(message.message_id) }
end
context "merges custom sparkpost header" do
before do
ActionMailer::Base.smtp_settings[:address] = "smtp.sparkpostmail.com"
message.header['X-MSYS-API'] = { foo: "bar" }.to_json
end
When { email_sender.send }
Then { expect(message.header['X-MSYS-API'].to_s).to match(message.message_id) }
end
context 'email logs' do
let(:email_log) { EmailLog.last }