helo-email-sdk 1.0.1 → 1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: afb5076cf98aa9c91576e55fe946cd31ec2c3db4f2c1b7d9fa57d1d0f73a39e7
4
- data.tar.gz: 404f3097b870c14fb708f09509eee2663a42b50bfb20f262a868c22a0188a5e7
3
+ metadata.gz: 24f067959418c6bacc218f81802bbcbda0eacedd25e5bc16f156c4e6e7ef2192
4
+ data.tar.gz: 1ee808b62e899e0df4a98e9c24385f5045df97daba11d55301a3312fb064bb03
5
5
  SHA512:
6
- metadata.gz: 25922786dfda9de69a3941b1b6ae32c9a92f8a84b53404d90d29632374cab2683f65410c05775a5bed9920020b04cd10ace4cd890b926a2d043b37f41f73470f
7
- data.tar.gz: 4915fa1d5b7ea81981887177c13dfc43dda6384cc84d86ba2f32e239e83db459bdfaef08cb1ffe0fcbaee2977a772d40da3c9562771080dcdcdf02511b6b449c
6
+ metadata.gz: b06dc543d3d8773f415b3d0413c5524006742af0a6a6cb74a750a8e4ac9a552f83e24d631917b84cafb3810022694e953ab7a3034d6e6ab40a16d724afddd389
7
+ data.tar.gz: f07bba17cd695cb7728cef33bc396222c78208d541c0e331a15e8abff22b9beb77ee3bfdc4c00ae539618343ab994265822e8239294041c54f09be33d4a1a9bb
data/README.md CHANGED
@@ -25,7 +25,6 @@ require "helo-email-sdk"
25
25
 
26
26
  Helo.configure do |config|
27
27
  config.api_key = ENV.fetch("HELO_API_KEY")
28
- config.base_url = "https://api.helohq.com" # optional, this is the default
29
28
  end
30
29
  ```
31
30
 
@@ -60,6 +59,52 @@ rescue Helo::APIError => e
60
59
  end
61
60
  ```
62
61
 
62
+ ## Rails (ActionMailer)
63
+
64
+ The gem registers a `:helo` delivery method with ActionMailer, so existing
65
+ mailers send through the API without any changes. Configure the SDK in an initializer:
66
+
67
+ ```ruby
68
+ # config/initializers/helo.rb
69
+ Helo.configure do |config|
70
+ config.api_key = Rails.application.credentials.helo_api_key
71
+ end
72
+ ```
73
+
74
+ and switch the delivery method:
75
+
76
+ ```ruby
77
+ # config/environments/production.rb
78
+ config.action_mailer.delivery_method = :helo
79
+ ```
80
+
81
+ Plain ActionMailer fields map to the API: `from`, `to`, `cc`, `bcc`, `reply_to`, `subject`, the
82
+ text and HTML parts, attachments (`attachments.inline` ones keep their content id, so
83
+ `image_tag attachments["logo.png"].url` works), and any custom headers. API-specific options go
84
+ in `delivery_method_options`, which other delivery methods (`:test`, letter_opener, SMTP) ignore:
85
+
86
+ ```ruby
87
+ class UserMailer < ApplicationMailer
88
+ def welcome(user)
89
+ headers["X-Custom-Header"] = "value" # custom headers are sent as-is
90
+ mail(
91
+ to: user.email,
92
+ subject: "Welcome!",
93
+ delivery_method_options: {
94
+ tags: ["welcome"],
95
+ metadata: { user_id: user.id.to_s },
96
+ tracking: { opens: true, links: false },
97
+ idempotency_key: "welcome-#{user.id}", # retries won't send twice
98
+ channel_id: "..."
99
+ }
100
+ )
101
+ end
102
+ end
103
+ ```
104
+
105
+ After delivery, `message[:helo_message_id].value` is the id the API assigned the
106
+ message, and failed requests raise `Helo::APIError` (subject to `config.action_mailer.raise_delivery_errors`).
107
+
63
108
  ## Webhook signature verification
64
109
 
65
110
  Webhook deliveries are signed with the endpoint's signing key. Verify every delivery before
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Helo
4
+ # ActionMailer delivery method, registered as :helo by the Railtie.
5
+ class Mailer
6
+ STANDARD_HEADERS = %w[
7
+ from to cc bcc reply-to subject date message-id mime-version
8
+ content-type content-transfer-encoding content-disposition content-id
9
+ sender return-path
10
+ ].freeze
11
+
12
+ # Where the API's id for the message is recorded after sending
13
+ MESSAGE_ID_FIELD = "helo-message-id"
14
+
15
+ attr_reader :settings
16
+
17
+ # Receives mail(delivery_method_options: { ... }) as settings. return_response makes
18
+ # Mail::Message#deliver! return our response.
19
+ def initialize(settings = {})
20
+ @settings = settings.merge(return_response: true)
21
+ end
22
+
23
+ def deliver!(mail)
24
+ raise ArgumentError, "Helo is not configured: call Helo.configure" unless API.default_client
25
+
26
+ response = Sending.send_transactional(
27
+ build_request(mail),
28
+ channel_id: settings[:channel_id],
29
+ idempotency_key: settings[:idempotency_key]
30
+ )
31
+ if response&.message_id
32
+ mail[MESSAGE_ID_FIELD] = nil # Mail would otherwise add a second field when resending
33
+ mail[MESSAGE_ID_FIELD] = response.message_id
34
+ end
35
+ response
36
+ end
37
+
38
+ private
39
+
40
+ def build_request(mail)
41
+ {
42
+ from: addresses(mail[:from]).first,
43
+ to: addresses(mail[:to]),
44
+ cc: addresses(mail[:cc]),
45
+ bcc: addresses(mail[:bcc]),
46
+ reply_to: addresses(mail[:reply_to]),
47
+ subject: mail.subject,
48
+ html: body(mail, "text/html"),
49
+ text: body(mail, "text/plain"),
50
+ attachments: attachments(mail),
51
+ headers: custom_headers(mail),
52
+ tags: settings[:tags],
53
+ metadata: settings[:metadata],
54
+ tracking: settings[:tracking]
55
+ }.compact_blank
56
+ end
57
+
58
+ def addresses(field)
59
+ return [] unless field
60
+ raise ArgumentError, "Invalid #{field.name} address: #{field.value}" unless field.respond_to?(:addrs)
61
+
62
+ field.addrs.map { |address| { email: address.address, name: address.display_name }.compact }
63
+ end
64
+
65
+ def body(mail, mime_type)
66
+ if mail.multipart?
67
+ part = mime_type == "text/html" ? mail.html_part : mail.text_part
68
+ part&.decoded
69
+ elsif (mail.mime_type || "text/plain") == mime_type
70
+ mail.decoded
71
+ end
72
+ end
73
+
74
+ def attachments(mail)
75
+ mail.attachments.map do |attachment|
76
+ {
77
+ content: [attachment.decoded].pack("m0"),
78
+ file_name: attachment.filename,
79
+ content_type: attachment.mime_type,
80
+ disposition: attachment.inline? ? "inline" : "attachment",
81
+ content_id: (attachment.cid if attachment.inline?)
82
+ }.compact
83
+ end
84
+ end
85
+
86
+ def custom_headers(mail)
87
+ mail.header_fields.each_with_object({}) do |field, headers|
88
+ next if STANDARD_HEADERS.include?(field.name.downcase) || field.name.downcase == MESSAGE_ID_FIELD
89
+
90
+ headers[field.name] = field.value.to_s
91
+ end
92
+ end
93
+ end
94
+ end
@@ -4,6 +4,7 @@ module Helo
4
4
  class SendBroadcastRequestTemplate
5
5
  include Helo::Core::Model
6
6
 
7
+ api_attribute :id, :string, key: "id"
7
8
  api_attribute :subject, :string, key: "subject"
8
9
  api_attribute :html, :string, key: "html"
9
10
  api_attribute :text, :string, key: "text"
@@ -4,6 +4,7 @@ module Helo
4
4
  class SendMessageRequestTemplate
5
5
  include Helo::Core::Model
6
6
 
7
+ api_attribute :id, :string, key: "id"
7
8
  api_attribute :subject, :string, key: "subject"
8
9
  api_attribute :html, :string, key: "html"
9
10
  api_attribute :text, :string, key: "text"
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Helo
4
+ class Railtie < ::Rails::Railtie
5
+ ActiveSupport.on_load(:action_mailer) do
6
+ add_delivery_method :helo, Helo::Mailer
7
+ end
8
+ end
9
+ end
data/lib/helo/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Helo
4
- VERSION = "1.0.1"
4
+ VERSION = "1.1.0"
5
5
  end
data/lib/helo.rb CHANGED
@@ -18,6 +18,8 @@ require_relative "helo/api_error"
18
18
  require_relative "helo/configuration"
19
19
  require_relative "helo/client"
20
20
  require_relative "helo/webhook_signatures"
21
+ require_relative "helo/mailer"
22
+ require_relative "helo/railtie" if defined?(Rails::Railtie)
21
23
 
22
24
 
23
25
  module Helo
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: helo-email-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Helo Email
@@ -37,6 +37,20 @@ dependencies:
37
37
  - - ">="
38
38
  - !ruby/object:Gem::Version
39
39
  version: '2.0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: actionmailer
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '7.0'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '7.0'
40
54
  - !ruby/object:Gem::Dependency
41
55
  name: minitest
42
56
  requirement: !ruby/object:Gem::Requirement
@@ -117,6 +131,7 @@ files:
117
131
  - lib/helo/enums/message_status.rb
118
132
  - lib/helo/enums/suppression_reason.rb
119
133
  - lib/helo/enums/webhook_event.rb
134
+ - lib/helo/mailer.rb
120
135
  - lib/helo/models/activity_event.rb
121
136
  - lib/helo/models/activity_list_events_request.rb
122
137
  - lib/helo/models/activity_list_messages_request.rb
@@ -231,6 +246,7 @@ files:
231
246
  - lib/helo/models/webhook_payload_common.rb
232
247
  - lib/helo/models/webhook_response.rb
233
248
  - lib/helo/models/webhooks_list_request.rb
249
+ - lib/helo/railtie.rb
234
250
  - lib/helo/version.rb
235
251
  - lib/helo/webhook_signatures.rb
236
252
  homepage: https://rubygems.org/gems/helo-email-sdk