rails_mailersend 0.4.1 → 0.6.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 877745c41ce75703b82b4b6dc8bbb34630a510a193d51b5ce55ebb65451d3545
4
- data.tar.gz: 872bb9d2cf47dd7d9ee90e7f5f899157822b09bfbb6b3aad3f3ea028a9fe79bf
3
+ metadata.gz: f3615a45b8f5ec8f462c1a590064cd923ff3e5ede15dcfa3470aeb83077a7858
4
+ data.tar.gz: 8f67af0e86500603cc5216f2504d06a4b3f7ba6b4903e270b3e3df59076be672
5
5
  SHA512:
6
- metadata.gz: aacb555e2d9c009d1225661ab1146e6c15257fb5b0687678c12a8a95f47929eb672b89e5df3b142b65c0d6aaddd0188252ffde4bb093c7d1bd1a34e46215a899
7
- data.tar.gz: 3843ec66ec35a363ca0f0f1f4592a75cb866833b8c20d09e9c7a9ab64c37bb1e833fc791ac590cf37847f7c43636b79d6bb42cd85ecc6e2a309bffdfd5c3dcb9
6
+ metadata.gz: d443297aedd5a29abb72ad9ef7bb74cd021bdb208e1998fde63c2e3553bb8951f4249a80cf723a2b3008cc2185960937361c6a7d83a90afca94ff8835bdcebfb
7
+ data.tar.gz: b40dc56a2d2322607c25d51df1a2eb18af3b0cafeff698944a863631c43dbc548d278a00c7e31e5273bc17416c9a3f68aca99c32a0f0ea7c5e2ab9b63f915625
data/README.md CHANGED
@@ -65,6 +65,55 @@ than returning quietly, so the enqueuing job retries and the failure is visible.
65
65
  For an app where sign-in is by magic link, a swallowed delivery error looks
66
66
  exactly like a broken app to the person waiting.
67
67
 
68
+ `In-Reply-To` and `References` are carried across to MailerSend's own
69
+ `in_reply_to` and `references` fields, so a reply lands under the message it
70
+ answers instead of opening a conversation of its own:
71
+
72
+ ```ruby
73
+ mail(to: person.email_address, subject: "Re: #{parent_subject}",
74
+ in_reply_to: parent_message_id, references: [parent_message_id])
75
+ ```
76
+
77
+ Angle brackets are put back on, since the `mail` gem strips them and MailerSend
78
+ validates against the RFC 5322 form. Every other header is dropped: the API takes
79
+ fields rather than a MIME message, and these are the two worth translating. Both
80
+ are paid-plan only at MailerSend, which answers a free account carrying them with
81
+ a 422 — loudly, rather than with an unthreaded reply nobody can account for.
82
+
83
+ ## Link and open tracking
84
+
85
+ MailerSend can rewrite every link in a message to a redirector on its own domain. For
86
+ marketing mail that is the point. For a message carrying a credential it is two problems at
87
+ once: the token is handed to a third party and kept in their click reporting, and the
88
+ rewritten URL is no longer yours — so an iOS Universal Link stops matching your
89
+ `apple-app-site-association` and opens a browser instead of your app. For a single-use
90
+ sign-in link that means it is spent somewhere the app cannot see it.
91
+
92
+ Set the switches per message, on the mailer that needs them:
93
+
94
+ ```ruby
95
+ class SessionMailer < ApplicationMailer
96
+ def sign_in(user)
97
+ headers["X-MailerSend-Track-Clicks"] = "false"
98
+ mail to: user.email, subject: "Your sign-in link"
99
+ end
100
+ end
101
+ ```
102
+
103
+ | Header | MailerSend field |
104
+ |---|---|
105
+ | `X-MailerSend-Track-Clicks` | `settings.track_clicks` |
106
+ | `X-MailerSend-Track-Opens` | `settings.track_opens` |
107
+ | `X-MailerSend-Track-Content` | `settings.track_content` |
108
+
109
+ Each takes `"true"` or `"false"`, case-insensitively. A message that sets none says nothing
110
+ about tracking, and MailerSend falls back to the domain's own setting — so this changes
111
+ nothing for the mail you already send.
112
+
113
+ A value that is neither raises, rather than being quietly dropped. The caller asking for
114
+ tracking off is usually sending a credential, and the failure mode of guessing is a token
115
+ routed through a redirector without anybody noticing.
116
+
68
117
  ## Inbound mail
69
118
 
70
119
  Optional, and only loads when the app has Action Mailbox. MailerSend posts the
@@ -29,6 +29,24 @@ module MailersendRails
29
29
  OPEN_TIMEOUT = 15
30
30
  READ_TIMEOUT = 30
31
31
 
32
+ # MailerSend's per-message tracking switches, and the header each is set from.
33
+ #
34
+ # Tracking rewrites every link in the message to a redirector on MailerSend's
35
+ # domain. For marketing mail that is the point. For a message carrying a
36
+ # credential it is two problems: the token is handed to a third party and
37
+ # logged in their click reporting, and the rewritten URL is no longer yours --
38
+ # so an iOS Universal Link stops matching your apple-app-site-association and
39
+ # opens a browser instead of your app, which for a single-use sign-in link
40
+ # means it is spent somewhere the app cannot see.
41
+ #
42
+ # Absent means absent: MailerSend falls back to the domain's own setting, so a
43
+ # message that says nothing keeps whatever the account is configured for.
44
+ TRACKING = {
45
+ "track_clicks" => "X-MailerSend-Track-Clicks",
46
+ "track_opens" => "X-MailerSend-Track-Opens",
47
+ "track_content" => "X-MailerSend-Track-Content"
48
+ }.freeze
49
+
32
50
  attr_reader :settings
33
51
 
34
52
  def initialize(settings = {})
@@ -55,10 +73,63 @@ module MailersendRails
55
73
  "reply_to" => Array(mail.reply_to).empty? ? {} : address_in(mail[:reply_to]),
56
74
  "subject" => mail.subject,
57
75
  "text" => mail.text_part&.body&.decoded,
58
- "html" => html_for(mail)
76
+ "html" => html_for(mail),
77
+ "in_reply_to" => message_ids_in(mail[:in_reply_to]).first,
78
+ "references" => message_ids_in(mail[:references]),
79
+ "settings" => tracking_in(mail)
59
80
  }.reject { |_, value| omit?(value) }
60
81
  end
61
82
 
83
+ # The tracking switches this message sets, if any. See TRACKING.
84
+ #
85
+ # A value that is neither "true" nor "false" raises rather than being
86
+ # dropped, for the same reason a failed delivery raises: the caller that
87
+ # asked for tracking off is usually sending a credential, and the failure
88
+ # mode of guessing is a token quietly routed through a redirector. A typo
89
+ # here is a mistake in the sender's own code and shows up the first time
90
+ # that mailer is exercised.
91
+ def tracking_in(mail)
92
+ TRACKING.each_with_object({}) do |(field, header), settings|
93
+ raw = mail[header]&.to_s&.strip
94
+ next if raw.nil? || raw.empty?
95
+
96
+ case raw.downcase
97
+ when "true" then settings[field] = true
98
+ when "false" then settings[field] = false
99
+ else raise DeliveryError, "#{header} must be \"true\" or \"false\", got #{raw.inspect}"
100
+ end
101
+ end
102
+ end
103
+
104
+ # In-Reply-To and References, in the form MailerSend asks for them.
105
+ #
106
+ # Threading is what puts a reply under the message it answers rather than
107
+ # in a conversation of its own, and these two headers are the whole of it.
108
+ # They need carrying by hand because the API takes fields rather than a
109
+ # MIME message: every header Action Mailer set that isn't named in
110
+ # #payload_for is dropped here, and a mailer that sets In-Reply-To and
111
+ # then watches its reply arrive as a new thread has nowhere to look for
112
+ # where it went. Both fields are paid-plan only at MailerSend, which
113
+ # answers a request carrying them on a free account with a 422 -- loudly,
114
+ # like every other delivery failure.
115
+ #
116
+ # The `mail` gem hands ids back with the angle brackets stripped and
117
+ # MailerSend validates against the RFC 5322 form, so they go back on. A
118
+ # field too malformed to parse falls back to splitting on whitespace,
119
+ # which is the shape of both headers.
120
+ def message_ids_in(field)
121
+ return [] if field.nil?
122
+
123
+ ids = field.respond_to?(:message_ids) ? field.message_ids : field.to_s.split
124
+ Array(ids).filter_map { |id| bracketed(id) }
125
+ end
126
+
127
+ def bracketed(id)
128
+ id = id.to_s.strip.delete_prefix("<").delete_suffix(">").strip
129
+
130
+ "<#{id}>" unless id.empty?
131
+ end
132
+
62
133
  def html_for(mail)
63
134
  mail.html_part&.body&.decoded || mail.body.decoded
64
135
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MailersendRails
4
- VERSION = "0.4.1"
4
+ VERSION = "0.6.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_mailersend
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Loman