resend 0.16.0 → 0.17.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 67fb549176718ac10025dad2b41cba2519de04a0890631950c4f2d022e6106f8
4
- data.tar.gz: de3d5e59326e9ae9fdd1e5d4d0b507a147ecc95aa3ed6db918e4a5f40894d782
3
+ metadata.gz: 6a332acce7880b6cbf53de706ef39df76d4823b588b1577a6642105ca00f3649
4
+ data.tar.gz: 5fdc46d025cb2377502e6f9a5a9a4ba605f279616f36dc7e537bb79d6f0c0413
5
5
  SHA512:
6
- metadata.gz: 77ae6d4b92169173fef0d18661de7e873fdb6c2e33291a0676a5a2ef622845bec3bfff815e63fda76ec6c5f8fb5f267df60b64fa62812544e100ceaa79bd916b
7
- data.tar.gz: a2dbe3619bf9958234a96299909937de98a752fe61678fb8d0082d83ba845828ca12fdc593b92c52fffbf98dd9139db9283957a45659aa8b4aba2d4ae5582c9b
6
+ metadata.gz: 1b3bbdc8c899628541a77cecb10d679afe0ca31f18bb80ad8eabedfbcdf3d7851153515387084cd003e54500fc7fa1b07f6f50a9c8a9eb9bd0d122df2199d73d
7
+ data.tar.gz: 123206e9012019d7d1274b97daeb83f5f093234ae6f0b44f6f888ed89a17d2b03d8dce234b0b811ae9e8fb6568ef1f0712d0ccc3385278a929fa09b53974916e
data/lib/resend/mailer.rb CHANGED
@@ -7,6 +7,17 @@ module Resend
7
7
  class Mailer
8
8
  attr_accessor :config, :settings
9
9
 
10
+ # These are set as `headers` by the Rails API, but these will be filtered out
11
+ # when constructing the Resend API payload, since they're are sent as post params.
12
+ # https://resend.com/docs/api-reference/emails/send-email
13
+ IGNORED_HEADERS = %w[
14
+ cc bcc
15
+ from reply-to to subject mime-version
16
+ html text
17
+ content-type tags scheduled_at
18
+ headers
19
+ ].freeze
20
+
10
21
  def initialize(config)
11
22
  @config = config
12
23
  raise Resend::Error.new("Make sure your API Key is set", @config) unless Resend.api_key
@@ -51,7 +62,19 @@ module Resend
51
62
  end
52
63
 
53
64
  #
54
- # Add custom headers fields
65
+ # Add custom headers fields.
66
+ #
67
+ # Both ways are supported:
68
+ #
69
+ # 1. Through the `#mail()` method ie:
70
+ # mail(headers: { "X-Custom-Header" => "value" })
71
+ #
72
+ # 2. Through the Rails `#headers` method ie:
73
+ # headers["X-Custom-Header"] = "value"
74
+ #
75
+ #
76
+ # setting the header values through the `#mail` method will overwrite values set
77
+ # through the `#headers` method using the same key.
55
78
  #
56
79
  # @param Mail mail Rails Mail object
57
80
  #
@@ -59,7 +82,39 @@ module Resend
59
82
  #
60
83
  def get_headers(mail)
61
84
  params = {}
62
- params[:headers] = mail[:headers].unparsed_value if mail[:headers].present?
85
+
86
+ if mail[:headers].present? || unignored_headers(mail).present?
87
+ params[:headers] = {}
88
+ params[:headers].merge!(headers_values(mail)) if unignored_headers(mail).present?
89
+ params[:headers].merge!(mail_headers_values(mail)) if mail[:headers].present?
90
+ end
91
+
92
+ params
93
+ end
94
+
95
+ # Gets the values of the headers that are set through the `#mail` method
96
+ #
97
+ # @param Mail mail Rails Mail object
98
+ # @return Hash hash with mail headers values
99
+ def mail_headers_values(mail)
100
+ params = {}
101
+ mail[:headers].unparsed_value.each do |k, v|
102
+ params[k.to_s] = v
103
+ end
104
+ params
105
+ end
106
+
107
+ # Gets the values of the headers that are set through the `#headers` method
108
+ #
109
+ # @param Mail mail Rails Mail object
110
+ # @return Hash hash with headers values
111
+ def headers_values(mail)
112
+ params = {}
113
+ unignored_headers(mail).each do |h|
114
+ params[h.name.to_s] = h.unparsed_value
115
+ end
116
+ # remove nil header values
117
+ params.delete_if { |_k, v| v.nil? }
63
118
  params
64
119
  end
65
120
 
@@ -144,5 +199,16 @@ module Resend
144
199
  end
145
200
  attachments
146
201
  end
202
+
203
+ #
204
+ # Get all headers that are not ignored
205
+ #
206
+ # @param Mail mail
207
+ #
208
+ # @return Array headers
209
+ #
210
+ def unignored_headers(mail)
211
+ @unignored_headers ||= mail.header_fields.reject { |h| IGNORED_HEADERS.include?(h.name.downcase) }
212
+ end
147
213
  end
148
214
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Resend
4
- VERSION = "0.16.0"
4
+ VERSION = "0.17.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resend
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.0
4
+ version: 0.17.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derich Pacheco
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-12-24 00:00:00.000000000 Z
11
+ date: 2025-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty