From 1f6c0663e02b6db8f3c70f45e0b9cc7f75bc53b3 Mon Sep 17 00:00:00 2001 From: meyric Date: Fri, 18 Oct 2024 12:36:36 +0100 Subject: [PATCH] Fix: view_mail does not duplicate headers With a view mail we have to call `mail` twice, once to get the body of the email from the view and once again to override the format contents. We were passing in any custom headers for both calls which was resulting in duplicate headers in the message. By calling `mail` without the headers, we can get the body and not modify the headers, leaving that for the second call. Interestingly adding custom headers is pointless, as they will never end up in the email from Notify - we may want to stop messing with the headers at all - I think it is a hangover from the v1 implementation. This was kindly identified by @inulty-dfe in #162 who are testing the header Mail::Fields that are potentially duplicated. --- lib/mail/notify/mailer.rb | 7 +++++-- spec/mail/notify/mailer_spec.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) 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