diff --git a/README.md b/README.md index f3245c4..62a3b40 100644 --- a/README.md +++ b/README.md @@ -206,6 +206,8 @@ view mailers. replies from your users - `reference`: A unique identifier you can create if necessary. This reference identifies a single unique notification or a batch of notifications +- `one_click_unsubscribe_url`: The URL email client will POST to in order to + unsubscribe from the mailing list More information can be [found in the Notify docs](https://docs.notifications.service.gov.uk/ruby.html#send-an-email-arguments-personalisation-optional) diff --git a/lib/mail/notify/delivery_method.rb b/lib/mail/notify/delivery_method.rb index 4652d99..8a3a784 100644 --- a/lib/mail/notify/delivery_method.rb +++ b/lib/mail/notify/delivery_method.rb @@ -17,7 +17,8 @@ def deliver!(message) email_address: message.to.first, personalisation: message.personalisation, email_reply_to_id: message.reply_to_id, - reference: message.reference + reference: message.reference, + one_click_unsubscribe_url: message.one_click_unsubscribe_url } client.send_email(params.compact) diff --git a/lib/mail/notify/mailer.rb b/lib/mail/notify/mailer.rb index cd871fd..0ff250e 100644 --- a/lib/mail/notify/mailer.rb +++ b/lib/mail/notify/mailer.rb @@ -35,6 +35,7 @@ def template_mail(template_id, options) message.template_id = template_id message.reply_to_id = options[:reply_to_id] message.reference = options[:reference] + message.one_click_unsubscribe_url = options[:one_click_unsubscribe_url] message.personalisation = options[:personalisation] || {} diff --git a/lib/mail/notify/message.rb b/lib/mail/notify/message.rb index 4b897c7..b361c96 100644 --- a/lib/mail/notify/message.rb +++ b/lib/mail/notify/message.rb @@ -3,7 +3,7 @@ module Mail module Notify module Message - attr_accessor :template_id, :personalisation, :reply_to_id, :reference + attr_accessor :template_id, :personalisation, :reply_to_id, :reference, :one_click_unsubscribe_url end end end diff --git a/spec/mail/notify/delivery_method_spec.rb b/spec/mail/notify/delivery_method_spec.rb index b0cb41a..3ec18ad 100644 --- a/spec/mail/notify/delivery_method_spec.rb +++ b/spec/mail/notify/delivery_method_spec.rb @@ -177,12 +177,11 @@ end describe "Notify optional fields" do - describe "email_reply_to_id" do - it "is present in the API call when set" do + context "when no optional fields present" do + it "optional fields not present in the call to the Notify API" do message = TestMailer.with( template_id: "test-id", - to: "test.name@email.co.uk", - reply_to_id: "test-reply-to-id" + to: "test.name@email.co.uk" ).test_template_mail notifications_client = mock_notifications_client @@ -192,15 +191,17 @@ expect(notifications_client).to have_received(:send_email).with( template_id: "test-id", email_address: "test.name@email.co.uk", - personalisation: {}, - email_reply_to_id: "test-reply-to-id" + personalisation: {} ) end + end - it "is not present in the call to the Notify API when not set" do + describe "email_reply_to_id" do + it "is present in the API call when set" do message = TestMailer.with( template_id: "test-id", - to: "test.name@email.co.uk" + to: "test.name@email.co.uk", + reply_to_id: "test-reply-to-id" ).test_template_mail notifications_client = mock_notifications_client @@ -210,7 +211,8 @@ expect(notifications_client).to have_received(:send_email).with( template_id: "test-id", email_address: "test.name@email.co.uk", - personalisation: {} + personalisation: {}, + email_reply_to_id: "test-reply-to-id" ) end end @@ -234,11 +236,16 @@ reference: "test-reference" ) end + end + + describe "one_click_unsubscribe_url" do + it "is present in the API call when set" do + one_click_unsubscribe_url = "https://www.example.com/unsubscribe?opaque=123" - it "is not present in the call to the Notify API when not set" do message = TestMailer.with( template_id: "test-id", - to: "test.name@email.co.uk" + to: "test.name@email.co.uk", + one_click_unsubscribe_url: ).test_template_mail notifications_client = mock_notifications_client @@ -248,7 +255,8 @@ expect(notifications_client).to have_received(:send_email).with( template_id: "test-id", email_address: "test.name@email.co.uk", - personalisation: {} + personalisation: {}, + one_click_unsubscribe_url: ) end end diff --git a/spec/mail/notify/mailer_spec.rb b/spec/mail/notify/mailer_spec.rb index 00d852b..4a734c0 100644 --- a/spec/mail/notify/mailer_spec.rb +++ b/spec/mail/notify/mailer_spec.rb @@ -202,6 +202,22 @@ expect(message.reference).to eql("test-reference") end + + it "sets one_click_unsubscribe_url" do + one_click_unsubscribe_url = "https://www.example.com/unsubscribe?opaque=123" + + message_params = { + template_id: "template-id", + to: "test.name@email.co.uk", + subject: "Test subject", + reference: "test-reference", + one_click_unsubscribe_url: + } + + message = TestMailer.with(message_params).test_template_mail + + expect(message.one_click_unsubscribe_url).to eql(one_click_unsubscribe_url) + end end describe "#blank_allowed" do