diff --git a/lib/mail/notify/mailer.rb b/lib/mail/notify/mailer.rb index 9deab1f..0cd46bd 100644 --- a/lib/mail/notify/mailer.rb +++ b/lib/mail/notify/mailer.rb @@ -76,9 +76,12 @@ def view_mail(template_id, options) subject = options[:subject] headers = options.except(:personalisation, :reply_to_id, :reference) - # we have to render the view for the message and grab the raw source, then we set that as the + # We have to render the view for the message and grab the raw source, then we set that as the # body in the personalisation for sending to the Notify API. - body = mail(headers).body.raw_source + # + # We do not pass the headers as the call to `mail` will keep adding headers resulting in + # duplication when we have to call it again later. + body = mail.body.raw_source # The 'view mail' works by sending a subject and body as personalisation options, these are # then used in the Notify template to provide content. diff --git a/spec/mail/notify/mailer_spec.rb b/spec/mail/notify/mailer_spec.rb index 622c89d..d72c68b 100644 --- a/spec/mail/notify/mailer_spec.rb +++ b/spec/mail/notify/mailer_spec.rb @@ -132,6 +132,20 @@ expect(message.header[:reply_to_id]).to be_nil expect(message.header[:reference]).to be_nil end + + it "sets custom headers only once" do + message_params = { + template_id: "template-id", + to: "test.name@email.co.uk", + subject: "Test subject", + custom_header: "custom header value" + } + + message = TestMailer.with(message_params).test_view_mail + + expect(message.header["custom-header"]).to be_a(Mail::Field) + expect(message.header["custom-header"].value).to eq("custom header value") + end end describe "#template_email" do @@ -236,6 +250,20 @@ expect(message.header[:reply_to_id]).to be_nil expect(message.header[:reference]).to be_nil end + + it "sets custom headers only once" do + message_params = { + template_id: "template-id", + to: "test.name@email.co.uk", + subject: "Test subject", + custom_header: "custom header value" + } + + message = TestMailer.with(message_params).test_view_mail + + expect(message.header["custom-header"]).to be_a(Mail::Field) + expect(message.header["custom-header"].value).to eq("custom header value") + end end describe "#blank_allowed" do