Skip to content

Commit bc8c25a

Browse files
committed
support one click unsubscribe
1 parent e5fc9b5 commit bc8c25a

File tree

6 files changed

+42
-14
lines changed

6 files changed

+42
-14
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ view mailers.
206206
replies from your users
207207
- `reference`: A unique identifier you can create if necessary. This reference
208208
identifies a single unique notification or a batch of notifications
209+
- `one_click_unsubscribe_url`: The URL email client will POST to in order to
210+
unsubscribe from the mailing list
209211

210212
More information can be [found in the
211213
Notify docs](https://docs.notifications.service.gov.uk/ruby.html#send-an-email-arguments-personalisation-optional)

lib/mail/notify/delivery_method.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ def deliver!(message)
1717
email_address: message.to.first,
1818
personalisation: message.personalisation,
1919
email_reply_to_id: message.reply_to_id,
20-
reference: message.reference
20+
reference: message.reference,
21+
one_click_unsubscribe_url: message.one_click_unsubscribe_url
2122
}
2223

2324
client.send_email(params.compact)

lib/mail/notify/mailer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def template_mail(template_id, options)
3535
message.template_id = template_id
3636
message.reply_to_id = options[:reply_to_id]
3737
message.reference = options[:reference]
38+
message.one_click_unsubscribe_url = options[:one_click_unsubscribe_url]
3839

3940
message.personalisation = options[:personalisation] || {}
4041

lib/mail/notify/message.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module Mail
44
module Notify
55
module Message
6-
attr_accessor :template_id, :personalisation, :reply_to_id, :reference
6+
attr_accessor :template_id, :personalisation, :reply_to_id, :reference, :one_click_unsubscribe_url
77
end
88
end
99
end

spec/mail/notify/delivery_method_spec.rb

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,11 @@
177177
end
178178

179179
describe "Notify optional fields" do
180-
describe "email_reply_to_id" do
181-
it "is present in the API call when set" do
180+
context "when no optional fields present" do
181+
it "optional fields not present in the call to the Notify API" do
182182
message = TestMailer.with(
183183
template_id: "test-id",
184-
to: "test.name@email.co.uk",
185-
reply_to_id: "test-reply-to-id"
184+
to: "test.name@email.co.uk"
186185
).test_template_mail
187186

188187
notifications_client = mock_notifications_client
@@ -192,15 +191,17 @@
192191
expect(notifications_client).to have_received(:send_email).with(
193192
template_id: "test-id",
194193
email_address: "test.name@email.co.uk",
195-
personalisation: {},
196-
email_reply_to_id: "test-reply-to-id"
194+
personalisation: {}
197195
)
198196
end
197+
end
199198

200-
it "is not present in the call to the Notify API when not set" do
199+
describe "email_reply_to_id" do
200+
it "is present in the API call when set" do
201201
message = TestMailer.with(
202202
template_id: "test-id",
203-
to: "test.name@email.co.uk"
203+
to: "test.name@email.co.uk",
204+
reply_to_id: "test-reply-to-id"
204205
).test_template_mail
205206

206207
notifications_client = mock_notifications_client
@@ -210,7 +211,8 @@
210211
expect(notifications_client).to have_received(:send_email).with(
211212
template_id: "test-id",
212213
email_address: "test.name@email.co.uk",
213-
personalisation: {}
214+
personalisation: {},
215+
email_reply_to_id: "test-reply-to-id"
214216
)
215217
end
216218
end
@@ -234,11 +236,16 @@
234236
reference: "test-reference"
235237
)
236238
end
239+
end
240+
241+
describe "one_click_unsubscribe_url" do
242+
it "is present in the API call when set" do
243+
one_click_unsubscribe_url = "https://www.example.com/unsubscribe?opaque=123"
237244

238-
it "is not present in the call to the Notify API when not set" do
239245
message = TestMailer.with(
240246
template_id: "test-id",
241-
to: "test.name@email.co.uk"
247+
to: "test.name@email.co.uk",
248+
one_click_unsubscribe_url:
242249
).test_template_mail
243250

244251
notifications_client = mock_notifications_client
@@ -248,7 +255,8 @@
248255
expect(notifications_client).to have_received(:send_email).with(
249256
template_id: "test-id",
250257
email_address: "test.name@email.co.uk",
251-
personalisation: {}
258+
personalisation: {},
259+
one_click_unsubscribe_url:
252260
)
253261
end
254262
end

spec/mail/notify/mailer_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,22 @@
202202

203203
expect(message.reference).to eql("test-reference")
204204
end
205+
206+
it "sets one_click_unsubscribe_url" do
207+
one_click_unsubscribe_url = "https://www.example.com/unsubscribe?opaque=123"
208+
209+
message_params = {
210+
template_id: "template-id",
211+
to: "test.name@email.co.uk",
212+
subject: "Test subject",
213+
reference: "test-reference",
214+
one_click_unsubscribe_url:
215+
}
216+
217+
message = TestMailer.with(message_params).test_template_mail
218+
219+
expect(message.one_click_unsubscribe_url).to eql(one_click_unsubscribe_url)
220+
end
205221
end
206222

207223
describe "#blank_allowed" do

0 commit comments

Comments
 (0)