mail-notify 1.2.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,11 +1,14 @@
1
- [![Build Status](https://github.com/dxw/mail-notify/workflows/Build/badge.svg)](https://github.com/dxw/mail-notify/actions)
2
- [![Coverage Status](https://img.shields.io/coveralls/github/pezholio/mail-notify.svg?style=flat-square)](https://coveralls.io/github/pezholio/mail-notify)
1
+ [![Build status](https://github.com/dxw/mail-notify/actions/workflows/unit-tests.yml/badge.svg)](https://github.com/dxw/mail-notify/actions/workflows/unit-tests.yml)
2
+ [![Coverage status](https://coveralls.io/repos/github/dxw/mail-notify/badge.svg?branch=fix-coveralls)](https://coveralls.io/github/dxw/mail-notify?branch=fix-coveralls)
3
3
  [![Gem Version](http://img.shields.io/gem/v/mail-notify.svg?style=flat-square)](https://rubygems.org/gems/mail-notify)
4
+ [![Rails integration tests](https://github.com/dxw/mail-notify/actions/workflows/rails-integration-tests.yml/badge.svg)](https://github.com/dxw/mail-notify/actions/workflows/rails-integration-tests.yml)
4
5
  [![License](http://img.shields.io/:license-mit-blue.svg)](https://mit-license.org/)
5
6
 
6
- # Mail::Notify
7
+ # mail-notify
7
8
 
8
- Rails / ActionMailer support for the [GOV.UK Notify API](https://www.notifications.service.gov.uk).
9
+ Rails plugin for [GOV.UK Notify](https://www.notifications.service.gov.uk).
10
+
11
+ [Great products and services like yours use mail-notify!](https://github.com/dxw/mail-notify/wiki#some-services-and-products-that-use-mail-notify)
9
12
 
10
13
  ## Installation
11
14
 
@@ -19,11 +22,10 @@ And then execute:
19
22
 
20
23
  $ bundle
21
24
 
22
- Or install it yourself as:
23
-
24
- $ gem install mail-notify
25
+ ## Configuration
25
26
 
26
- Then, add the following to your `config/environments/*.rb` (where * is `test`, `development`, `production` or whatever other environment(s) you have) file(s):
27
+ Configure in each environment `config/environments/*.rb` (where * is `test`,
28
+ `development`, `production` or whatever other environment(s) you have) file(s):
27
29
 
28
30
  ```ruby
29
31
  config.action_mailer.delivery_method = :notify
@@ -32,7 +34,13 @@ config.action_mailer.notify_settings = {
32
34
  }
33
35
  ```
34
36
 
35
- If you're using a different Notify service to GOV.UK Notify (for example [GOV.CA Notify](https://notification.alpha.canada.ca/)), you can also specify the Base URL in your setup:
37
+ We recommend using separate Notify 'test' API keys (email will not be sent but
38
+ can be seen in Notify) in all environments except production so you can confirm
39
+ the integration and generate previews without actually sending any email.
40
+
41
+ If you're using a different Notify service to GOV.UK Notify (for example [GOV.CA
42
+ Notify](https://notification.alpha.canada.ca/)), you can also specify the Base
43
+ URL in your setup:
36
44
 
37
45
  ```ruby
38
46
  config.action_mailer.delivery_method = :notify
@@ -44,131 +52,228 @@ config.action_mailer.notify_settings = {
44
52
 
45
53
  ### Mailers
46
54
 
47
- There are two options for using `Mail::Notify`, either templating in Rails with a view, or templating in Notify. Whichever way you choose, you'll need your mailers to inherit from `Mail::Notify::Mailer` like so:
55
+ There are two options for using mail-notify, manage the content in Notify
56
+ with [template mailers](#template-mailers) or in Rails with [view
57
+ mailers](#view-mailers) you can mix the two approaches as you need.
58
+
59
+ Whichever you choose, you'll need your mailers to inherit from `Mail::Notify::Mailer` like so:
48
60
 
49
61
  ```ruby
50
- class MyMailer < Mail::Notify::Mailer
62
+ class MyCustomMailer < Mail::Notify::Mailer
51
63
  end
52
64
  ```
53
65
 
54
- #### With a view
66
+ We recommend going 'all in' with Notify and having `ApplicationMailer` inherit
67
+ for mail-notify:
68
+
69
+ ```ruby
70
+ class ApplicationMailer < Mail::Notify::Mailer
71
+ end
72
+ ```
55
73
 
56
- Out of the box, Notify offers support for templating, with some rudimentary logic included. If you'd rather have your templating logic included with your source code for ease of access, or you want to do some more complex logic that's not supported by Notify, you can template your mailer views in erb.
74
+ then have each mailer inherit from `ApplicationMailer`:
57
75
 
58
- For this to work with Notify, you'll need a very simple template set up in Notify, with a `((subject))` variable in the subject line, and a `((body))` variable in the body field, as below:
76
+ ```ruby
77
+ class MyCustomMailer < ApplicationMailer
78
+ end
79
+ ```
59
80
 
60
- ![Example screenshot](docs/screenshot.png)
81
+ #### Template mailers
61
82
 
62
- Next, in your mailer you'll need to call `view_mail` with the first parameter being the ID of the notify template, followed by a hash of email headers e.g:
83
+ Template mailers only require the template ID from Notify and an to email
84
+ address, all of the content for the email is managed in Notify:
63
85
 
64
86
  ```ruby
65
- class MyMailer < Mail::Notify::Mailer
66
- def send_email
67
- view_mail('YOUR_TEMPLATE_ID_GOES_HERE',
68
- to: 'mail@somewhere.com',
69
- subject: 'Subject line goes here'
70
- )
87
+ class MyCustomMailer < ApplicationMailer
88
+ def welcome_email
89
+ to = params[:to]
90
+
91
+ template_mail("NOTIFY_TEMPLATE_ID", to: to)
71
92
  end
72
93
  end
73
- ```
74
-
75
- Your view can then be a simple `text.erb` file. You can add some markdown for headers, bullet points and links etc. These are handled in the same way as standard action_mailer views.
76
94
 
77
- #### With Notify templating
95
+ # call the template mailer
96
+ MyCustomMailer.with(to: "first.last@example.com").welcome_email.deliver_now!
97
+ ```
78
98
 
79
- You can also send your customisations in the more traditional way, and do your templating in Notify if you prefer. For this, you'll need to call `template_mail`, again with the first parameter being the ID of the template, and a hash of email headers, including your personalisations, e.g:
99
+ You can add any number of
100
+ [personalisations](https://www.notifications.service.gov.uk/using-notify/personalisation) to template mailers:
80
101
 
81
102
  ```ruby
82
- class MyMailer < Mail::Notify::Mailer
83
- def send_email
84
- template_mail('YOUR_TEMPLATE_ID_GOES_HERE',
85
- to: 'mail@somewhere.com',
86
- personalisation: {
87
- foo: 'bar'
88
- }
103
+ class MyCustomMailer < ApplicationMailer
104
+ def appointment_email
105
+ to = params[:to]
106
+ name = params[:name]
107
+ appointment_date = params[:appointment_date]
108
+
109
+ template_mail(
110
+ "NOTIFY_TEMPLATE_ID",
111
+ to: to,
112
+ personalisation: {
113
+ name: name,
114
+ appointment_date: date.to_s
115
+ }
89
116
  )
90
117
  end
91
118
  end
119
+
120
+ # call the template mailer with personalisation options
121
+ MyCustomMailer.with(
122
+ to: "first.last@example.com",
123
+ name: "First Last",
124
+ appointment_date: Date.new(2024, 01, 01)
125
+ ).appointment_email.deliver_now!
92
126
  ```
93
127
 
94
- By default, any blank personalisation are removed from the request, which will trigger mail template validation. This is to avoid accidental blanks in the email. If you want to send a blank value, you need to explicitly state that the personalization can be blank:
128
+ A note on blank personalisation; The Notify API will not allow `nil`
129
+ personalisation, if you expect `nil` values, you can wrap them in
130
+ `blank_allowed` which converts them to an empty string:
95
131
 
96
132
  ```ruby
97
- class MyMailer < Mail::Notify::Mailer
98
- def send_email
99
- template_mail('YOUR_TEMPLATE_ID_GOES_HERE',
100
- to: 'mail@somewhere.com',
101
- personalisation: {
102
- foo: foo.name, # This will trigger template validation error when blank
103
- bar: blank_allowed(bar.name) # This will inject empty string in the template when blank
104
- }
105
- )
106
- end
107
- end
133
+ MyCustomMailer.with(
134
+ to: "first.last@example.com", name: blank_allowed(user.name)).welcome_email.deliver_now!
108
135
  ```
109
136
 
137
+ Or use params as the examples above.
110
138
 
111
- #### With optional Notify arguments
139
+ #### View mailers
140
+
141
+ View mailers let you manage the content of emails with a Rails text view, with
142
+ Notify's markdown like
143
+ [formatting](https://www.notifications.service.gov.uk/using-notify/formatting)
144
+ supported.
112
145
 
113
- It's possible to pass two optional arguments to Notify:
146
+ You will still require a template in Notify, the template must be setup with
147
+ `subject` and `body` personalisations, which will be replaced with those from
148
+ your mailer and view:
114
149
 
115
- - `reply_to_id`: This is an email reply-to address specified by you to receive replies from your users
116
- - `reference`: A unique identifier you can create if necessary. This reference identifies a single unique notification or a batch of notifications
150
+ ![Screenshot of a view mailer template in Notify](docs/assets/images/view_template_in_notify.png)
117
151
 
118
- More information can be [found in the docs](https://docs.notifications.service.gov.uk/ruby.html#send-an-email-arguments-personalisation-optional)
152
+ Your view mailer is then setup like this:
119
153
 
120
154
  ```ruby
121
- class MyMailer < Mail::Notify::Mailer
122
- def send_email
123
- view_mail('YOUR_TEMPLATE_ID_GOES_HERE',
124
- to: 'mail@somewhere.com',
125
- subject: 'Subject line goes here',
126
- reply_to_id: 'YOUR_REPLY_TO_ID_GOES_HERE',
127
- reference: 'ABC123XYZ'
128
- )
155
+ class MyCustomMailer < ApplicationMailer
156
+ def welcome_email
157
+ to = params[:to]
158
+ subject= params[:subject]
159
+
160
+ view_mail("NOTIFY_TEMPLATE_ID", to: to, subject: subject)
129
161
  end
130
- end
131
162
  ```
132
163
 
133
- #### With Devise
164
+ With a `subject` being required.
134
165
 
135
- If you're using [Devise](https://github.com/heartcombo/devise), you can overwrite your Devise mailer to use mail-notify for password reset emails etc.
166
+ Add the view named appropriately and in the conventional location:
136
167
 
137
- In `config/initializers/devise.rb`:
168
+ `app/views/my_custom_mailer/welcome_email.text.erb`
169
+
170
+ Add content to the view:
171
+
172
+ ```
173
+ Dear <%= @user.name %>
174
+
175
+ # Welcome to the service.
176
+
177
+ Here are some points to note:
178
+
179
+ * point one
180
+ * point two
181
+ * point three
182
+
183
+ ^ Don't forget this.
184
+
185
+ ```
186
+
187
+ Then call the mailer as usual:
138
188
 
139
189
  ```ruby
140
- config.mailer = 'DeviseMailer'
190
+ MyCustomMailer.with(
191
+ to: "first.last@example.com",
192
+ subject: "Welcome to service"
193
+ ).welcome_email.deliver_now!
194
+
141
195
  ```
142
196
 
143
- in `app/mailers/devise_mailer.rb`:
197
+ Only plain text views can be used, with the Notify markdown like formatting
198
+ options. The email is sent as both HTML and plain text by Notify.
199
+
200
+ #### With optional Notify arguments
201
+
202
+ It's possible to pass two optional arguments to Notify with either template or
203
+ view mailers.
204
+
205
+ - `reply_to_id`: This is an email reply-to address specified by you to receive
206
+ replies from your users
207
+ - `reference`: A unique identifier you can create if necessary. This reference
208
+ 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
211
+
212
+ More information can be [found in the
213
+ Notify docs](https://docs.notifications.service.gov.uk/ruby.html#send-an-email-arguments-personalisation-optional)
144
214
 
145
215
  ```ruby
146
- class DeviseMailer < Devise::Mailer
147
- def devise_mail(record, action, opts = {}, &block)
148
- initialize_from_record(record)
149
- view_mail(ENV['NOTIFY_TEMPLATE_ID'], headers_for(action, opts))
150
- end
216
+ class MyCustomMailer < ApplicationMailer
217
+ def welcome_email
218
+ to = params[:to]
219
+ reference = params[:reference]
220
+ reply_to_id = params[:reply_to_id]
221
+
222
+ template_mail("NOTIFY_TEMPLATE_ID", to: to, reply_to_id: reply_to_id, reference: reference)
223
+ end
151
224
  end
225
+
226
+ # call the mailer
227
+ MyCustomMailer.with(
228
+ to: "first.last@example.com",
229
+ reference: "YOUR_REFERENCE",
230
+ reply_to_id: "YOUR_REPLY_TO"
231
+ ).welcome_email.deliver_now!
152
232
  ```
153
233
 
154
234
  ## Previews
155
235
 
156
- If you're using ActionMailer with Rails, [previews](https://guides.rubyonrails.org/action_mailer_basics.html#previewing-emails) are supported too, and work in the same way as standard previews. Currently they're shown without any branding, but this may change in future.
236
+ Rails [previews](https://guides.rubyonrails.org/action_mailer_basics.html#previewing-emails)
237
+ are supported.
238
+
239
+ The Rails delivery method must be set to `:notify` and a Notify API key will be
240
+ required for previews to work as the [preview is
241
+ generated](https://docs.notifications.service.gov.uk/ruby.html#generate-a-preview-template)
242
+ by the Notify API.
243
+
244
+ ## With Devise
245
+
246
+ Mail-notify is compatible with anything that uses ActionMailer,
247
+ [Devise](https://github.com/heartcombo/devise) is a popular authentication gem
248
+ that uses ActionMailer to send emails relating to
249
+ accounts, see [instructions in the
250
+ wiki](https://github.com/dxw/mail-notify/wiki/Use-with-Devise) for more details
251
+ of using mail-notify with Devise.
157
252
 
158
253
  ## Development
159
254
 
160
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
255
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
256
+ `bin/rspec` to run the tests. You can also run `bin/console` for an interactive
257
+ prompt that will allow you to experiment.
161
258
 
162
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
259
+ To release a new version, update the version number in `version.rb`, and then
260
+ tag the commit on main - the release will be built and published by the
261
+ `publish.yml` GitHub action.
163
262
 
164
263
  ## Contributing
165
264
 
166
- Bug reports and pull requests are welcome on GitHub at https://github.com/dxw/mail-notify. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
265
+ Bug reports and pull requests are welcome on GitHub at
266
+ https://github.com/dxw/mail-notify. This project is intended to be a safe,
267
+ welcoming space for collaboration, and contributors are expected to adhere to
268
+ the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
167
269
 
168
270
  ## License
169
271
 
170
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
272
+ The gem is available as open source under the terms of the [MIT
273
+ License](https://opensource.org/licenses/MIT).
171
274
 
172
275
  ## Code of Conduct
173
276
 
174
- Everyone interacting in the Mail::Notify project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/pezholio/mail-notify/blob/master/CODE_OF_CONDUCT.md).
277
+ Everyone interacting in the Mail::Notify project’s codebases, issue trackers,
278
+ chat rooms and mailing lists is expected to follow the [code of
279
+ conduct](https://github.com/pezholio/mail-notify/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile CHANGED
@@ -1,12 +1,7 @@
1
- # frozen_string_literal: true
2
-
3
1
  require "bundler/gem_tasks"
4
2
  require "rspec/core/rake_task"
5
3
  require "standard/rake"
6
- require "coveralls/rake/task"
7
-
8
- Coveralls::RakeTask.new
9
4
 
10
5
  RSpec::Core::RakeTask.new(:spec)
11
6
 
12
- task default: %i[standard spec coveralls:push]
7
+ task default: %i[standard spec]
@@ -1,14 +1,14 @@
1
- <%# Copied from GOV.UK Notify: https://raw.githubusercontent.com/alphagov/notifications-utils/d69de041d1be7b826e7d9cc56450e157ad361799/notifications_utils/jinja_templates/email_template.jinja2 %>
2
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
1
+ <!-- Template sourced from https://github.com/alphagov/notifications-utils/blob/main/notifications_utils/jinja_templates/email_template.jinja2 -->
2
+ <!DOCTYPE html>
3
+ <html lang="en">
4
4
 
5
5
  <head>
6
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
7
- <meta content="telephone=no" name="format-detection" /> <!-- need to add formatting for real phone numbers -->
8
- <meta name="viewport" content="width=device-width" />
9
- <title>Page title</title>
6
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
7
+ <meta content="telephone=no" name="format-detection"> <!-- need to add formatting for real phone numbers -->
8
+ <meta name="viewport" content="width=device-width">
9
+ <title>{{ subject }}</title>
10
10
 
11
- <style type="text/css">
11
+ <style>
12
12
  @media only screen and (min-device-width: 581px) {
13
13
  .content {
14
14
  width: 580px !important;
@@ -19,7 +19,7 @@
19
19
  </style>
20
20
 
21
21
  <!--[if gte mso 9]>
22
- <style type="text/css">
22
+ <style>
23
23
  li {
24
24
  margin-left: 4px !important;
25
25
  }
@@ -33,8 +33,7 @@
33
33
  </head>
34
34
 
35
35
  <body style="font-family: Helvetica, Arial, sans-serif;font-size: 16px;margin: 0;color:#0b0c0c;">
36
-
37
- <span style="display: none;font-size: 1px;color: #fff; max-height: 0;"></span>
36
+ <span style="display: none;font-size: 1px;color: #fff; max-height: 0;" hidden>{{ preheader }}…</span>
38
37
  <table role="presentation" width="100%" style="border-collapse: collapse;min-width: 100%;width: 100% !important;" cellpadding="0" cellspacing="0" border="0">
39
38
  <tr>
40
39
  <td width="100%" height="53" bgcolor="#0b0c0c">
@@ -51,20 +50,20 @@
51
50
  <tr>
52
51
  <td style="padding-left: 10px">
53
52
  <img
54
- src="https://static.notifications.service.gov.uk/images/gov.uk_logotype_crown.png"
55
- alt=" "
53
+ src="https://static.notifications.service.gov.uk/images/govuk-logotype-tudor-crown.png"
54
+ alt=""
56
55
  height="32"
57
56
  border="0"
58
- style="Margin-top: 4px;"
59
- />
57
+ style="Margin-top: 2px;"
58
+ >
60
59
  </td>
61
- <td style="font-size: 28px; line-height: 1.315789474; Margin-top: 4px; padding-left: 10px;">
60
+ <td style="font-size: 28px; line-height: 1.315789474; Margin-top: 4px; padding-left: 8px;">
62
61
  <span style="
63
62
  font-family: Helvetica, Arial, sans-serif;
64
63
  font-weight: 700;
65
64
  color: #ffffff;
66
65
  text-decoration: none;
67
- vertical-align:top;
66
+ vertical-align:middle;
68
67
  display: inline-block;
69
68
  ">GOV.UK</span>
70
69
  </td>
@@ -102,7 +101,7 @@
102
101
  <![endif]-->
103
102
  <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse: collapse;">
104
103
  <tr>
105
- <td bgcolor="#005EA5" width="100%" height="10"></td>
104
+ <td bgcolor="#1D70B8" width="100%" height="10"></td>
106
105
  </tr>
107
106
  </table>
108
107
  <!--[if (gte mso 9)|(IE)]>
@@ -114,7 +113,6 @@
114
113
  <td width="10" valign="middle" height="10"></td>
115
114
  </tr>
116
115
  </table>
117
-
118
116
  <table
119
117
  role="presentation"
120
118
  class="content"
@@ -126,27 +124,27 @@
126
124
  width="100%"
127
125
  >
128
126
  <tr>
129
- <td height="30"><br /></td>
127
+ <td height="30"><br></td>
130
128
  </tr>
131
129
  <tr>
132
- <td width="10" valign="middle"><br /></td>
130
+ <td width="10" valign="middle"><br></td>
133
131
  <td style="font-family: Helvetica, Arial, sans-serif; font-size: 19px; line-height: 1.315789474; max-width: 560px;">
134
132
  <!--[if (gte mso 9)|(IE)]>
135
133
  <table role="presentation" width="560" align="center" cellpadding="0" cellspacing="0" border="0" style="border-collapse: collapse;width: 560px;">
136
134
  <tr>
137
135
  <td style="font-family: Helvetica, Arial, sans-serif; font-size: 19px; line-height: 1.315789474;">
138
- <![endif]-->
139
- <%= yield %>
136
+ <![endif]-->
137
+ <%= yield %>
140
138
  <!--[if (gte mso 9)|(IE)]>
141
139
  </td>
142
140
  </tr>
143
141
  </table>
144
142
  <![endif]-->
145
143
  </td>
146
- <td width="10" valign="middle"><br /></td>
144
+ <td width="10" valign="middle"><br></td>
147
145
  </tr>
148
146
  <tr>
149
- <td height="30"><br /></td>
147
+ <td height="30"><br></td>
150
148
  </tr>
151
149
  </table>
152
150
  </body>
data/bin/console CHANGED
@@ -2,14 +2,9 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'bundler/setup'
5
+ require 'action_mailer'
6
+ require 'action_controller'
5
7
  require 'mail/notify'
8
+ require 'pry'
6
9
 
7
- # You can add fixtures and/or initialization code here to make experimenting
8
- # with your gem easier. You can also use a different console, if you like.
9
-
10
- # (If you use this, don't forget to add pry to your Gemfile!)
11
- # require "pry"
12
- # Pry.start
13
-
14
- require 'irb'
15
- IRB.start(__FILE__)
10
+ Pry.start
data/bin/rspec ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rspec' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
12
+
13
+ bundle_binstub = File.expand_path("bundle", __dir__)
14
+
15
+ if File.file?(bundle_binstub)
16
+ if File.read(bundle_binstub, 300).include?("This file was generated by Bundler")
17
+ load(bundle_binstub)
18
+ else
19
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
20
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
21
+ end
22
+ end
23
+
24
+ require "rubygems"
25
+ require "bundler/setup"
26
+
27
+ load Gem.bin_path("rspec-core", "rspec")
data/bin/standardrb ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'standardrb' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
12
+
13
+ bundle_binstub = File.expand_path("bundle", __dir__)
14
+
15
+ if File.file?(bundle_binstub)
16
+ if File.read(bundle_binstub, 300).include?("This file was generated by Bundler")
17
+ load(bundle_binstub)
18
+ else
19
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
20
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
21
+ end
22
+ end
23
+
24
+ require "rubygems"
25
+ require "bundler/setup"
26
+
27
+ load Gem.bin_path("standard", "standardrb")
@@ -6,20 +6,29 @@ module Mail
6
6
  attr_accessor :settings, :response
7
7
 
8
8
  def initialize(settings)
9
- raise ArgumentError, "You must specify an API key" if settings[:api_key].blank?
9
+ raise ArgumentError, "You must specify a Notify API key" if settings[:api_key].blank?
10
10
 
11
11
  @settings = settings
12
12
  end
13
13
 
14
- def deliver!(mail)
15
- @mail = mail
16
- @personalisation = Personalisation.new(mail)
17
- send_email
14
+ def deliver!(message)
15
+ params = {
16
+ template_id: message.template_id,
17
+ email_address: message.to.first,
18
+ personalisation: message.personalisation,
19
+ email_reply_to_id: message.reply_to_id,
20
+ reference: message.reference,
21
+ one_click_unsubscribe_url: message.one_click_unsubscribe_url
22
+ }
23
+
24
+ client.send_email(params.compact)
18
25
  end
19
26
 
20
- def preview(mail)
21
- personalisation = Personalisation.new(mail).to_h
22
- template_id = mail[:template_id].to_s
27
+ def preview(message)
28
+ template_id = message.template_id
29
+ personalisation = message.personalisation
30
+
31
+ Rails.logger.info("Getting Notify preview for template id #{template_id}")
23
32
  client.generate_template_preview(template_id, personalisation: personalisation)
24
33
  end
25
34
 
@@ -28,24 +37,6 @@ module Mail
28
37
  def client
29
38
  @client ||= Notifications::Client.new(@settings[:api_key], @settings[:base_url])
30
39
  end
31
-
32
- def email_params
33
- {
34
- email_address: @mail.to.first,
35
- template_id: @mail[:template_id].to_s,
36
- personalisation: @personalisation.to_h,
37
- email_reply_to_id: optional_param(:reply_to_id),
38
- reference: optional_param(:reference)
39
- }
40
- end
41
-
42
- def optional_param(name)
43
- @mail[name].presence&.to_s
44
- end
45
-
46
- def send_email
47
- @response = client.send_email(email_params.compact)
48
- end
49
40
  end
50
41
  end
51
42
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mail
4
+ module Notify
5
+ class Engine < Rails::Engine
6
+ initializer "mail-notify.add_delivery_method", before: "action_mailer.set_configs" do
7
+ ActionMailer::Base.add_delivery_method(:notify, Mail::Notify::DeliveryMethod)
8
+
9
+ config.action_mailer.preview_interceptors = [:mail_notify_preview_interceptor]
10
+ end
11
+ end
12
+ end
13
+ end