mailpace-rails 0.3.1 → 0.4.0

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: 810e29220dad031ea534f019a029ff4d476687b2fd1b32a52fec6c4560411ec8
4
- data.tar.gz: 4f989e26f0198db062f9ef63f1d4e31e03decad52fbd90b4a23589d59884b8f2
3
+ metadata.gz: 116f3c2d1a6a94ee97b33eccd6b576ffa86146e262fa18fea458fd30cc6ce9bb
4
+ data.tar.gz: 367ca296eff427a12a36a56096a04ebc0e694f635d9667c629db4b67c67f65bd
5
5
  SHA512:
6
- metadata.gz: ee5ae6d07eea2abc0b0455dece234474c4e32a27e8b32a615415baf5f7a87937eb8ec85eead066ab2f66111a033340272847acd8f7f3e6fe9dd5990e22412bc4
7
- data.tar.gz: 22fbd9a58c476999f6dd8d77fa186af27c2c5911d0b018ca5f13fc981d3e755e8ff6bb8f434327852d9f34c6e6c5349bd6ade43b26926127b41e7a32047f42bc
6
+ metadata.gz: 2c93b6b5261b781d2692f76732deea6940ab615229cb548986d44023245e5426fdb98937a046cb7dc7f57b5f3c14301107784b22aca641f6c5e10fc1c4ae3467
7
+ data.tar.gz: 9a4e461785290e63f2bced09602a6c9770ca747d120ba8ac1247d9fc7a792bf91347f74ef759473604baefdb46cdaf9a9cf56c351ba8e6b4d87b1db97d9c09ce
data/README.md CHANGED
@@ -8,6 +8,8 @@
8
8
 
9
9
  The MailPace Rails Gem is a plug in for ActionMailer to send emails via [MailPace](https://mailpace.com) to make sending emails from Rails apps super simple.
10
10
 
11
+ > **New in 0.4.0: [Idempotent Requests](https://docs.mailpace.com/guide/idempotency/), Improved Error Handling, InReplyTo and References support**
12
+
11
13
  > **New in 0.3.0: The ability to consume [inbound emails](https://docs.mailpace.com/guide/inbound/) from MailPace via ActionMailbox**
12
14
 
13
15
  ## Usage
@@ -133,6 +135,26 @@ Alternatively, provide the password in the `RAILS_INBOUND_EMAIL_PASSWORD` enviro
133
135
  `https://actionmailbox:PASSWORD@example.com/rails/action_mailbox/mailpace/inbound_emails`
134
136
 
135
137
  That's it! Emails should start flowing into your app just like magic.
138
+
139
+ ## Idempotent Requests
140
+
141
+ Mailpace supports [idempotency](https://docs.mailpace.com/guide/idempotency) for safely retrying requests without accidentally sending the same email twice. This is useful to guarantee that an email is not sent to the same recipient multiple times, e.g. through a network error, or a bug in your application logic.
142
+
143
+ To do this, when writing your mailer, generate and add a unique `idempotency_key`:
144
+
145
+ ```ruby
146
+ class TestMailer < ApplicationMailer
147
+ default from: 'notifications@example.com'
148
+ def idempotent_mail
149
+ email = 'email@example.com'
150
+ mail(
151
+ to: email,
152
+ idempotency_key: Digest::SHA256.hexdigest("#{email}-#{Time.now.to_i / 3600}")
153
+ )
154
+ end
155
+ end
156
+ ```
157
+
136
158
  ## Support
137
159
 
138
160
  For support please check the [MailPace Documentation](https://docs.mailpace.com) or contact us at support@mailpace.com
@@ -1,5 +1,5 @@
1
1
  module Mailpace
2
2
  module Rails
3
- VERSION = '0.3.1'
3
+ VERSION = '0.4.0'
4
4
  end
5
5
  end
@@ -21,16 +21,18 @@ module Mailpace
21
21
  result = HTTParty.post(
22
22
  'https://app.mailpace.com/api/v1/send',
23
23
  body: {
24
- from: mail.header[:from]&.address_list&.addresses&.first.to_s,
24
+ from: address_list(mail.header[:from])&.addresses&.first.to_s,
25
25
  to: mail.to.join(','),
26
26
  subject: mail.subject,
27
27
  htmlbody: mail.html_part ? mail.html_part.body.decoded : mail.body.to_s,
28
28
  textbody: if mail.multipart?
29
29
  mail.text_part ? mail.text_part.body.decoded : nil
30
30
  end,
31
- cc: mail.cc&.join(','),
32
- bcc: mail.bcc&.join(','),
31
+ cc: address_list(mail.header[:cc])&.addresses&.join(','),
32
+ bcc: address_list(mail.header[:bcc])&.addresses&.join(','),
33
33
  replyto: mail.reply_to&.join(','),
34
+ inreplyto: mail.header['In-Reply-To'].to_s,
35
+ references: mail.header['References'].to_s,
34
36
  list_unsubscribe: mail.header['list_unsubscribe'].to_s,
35
37
  attachments: format_attachments(mail.attachments),
36
38
  tags: mail.header['tags'].to_s
@@ -39,7 +41,8 @@ module Mailpace
39
41
  'User-Agent' => "MailPace Rails Gem v#{Mailpace::Rails::VERSION}",
40
42
  'Accept' => 'application/json',
41
43
  'Content-Type' => 'application/json',
42
- 'Mailpace-Server-Token' => settings[:api_token]
44
+ 'Mailpace-Server-Token' => settings[:api_token],
45
+ 'Idempotency-Key' => mail.header['idempotency_key'].to_s
43
46
  }
44
47
  )
45
48
 
@@ -63,9 +66,10 @@ module Mailpace
63
66
  def handle_response(result)
64
67
  return result unless result.code != 200
65
68
 
66
- # TODO: Improved error handling
67
- res = result.parsed_response
68
- raise res['error']&.to_s || res['errors']&.to_s
69
+ parsed_response = result.parsed_response
70
+ error_message = join_error_messages(parsed_response)
71
+
72
+ raise DeliveryError, "MAILPACE Error: #{error_message}" unless error_message.empty?
69
73
  end
70
74
 
71
75
  def format_attachments(attachments)
@@ -78,9 +82,25 @@ module Mailpace
78
82
  }.compact
79
83
  end
80
84
  end
85
+
86
+ def address_list(obj)
87
+ if obj&.respond_to?(:element)
88
+ # Mail 2.8+
89
+ obj.element
90
+ else
91
+ # Mail <= 2.7.x
92
+ obj&.address_list
93
+ end
94
+ end
95
+
96
+ def join_error_messages(response)
97
+ # Join 'error' and 'errors' keys from response into a single string
98
+ [response['error'], response['errors']].compact.join(', ')
99
+ end
81
100
  end
82
101
 
83
102
  class Error < StandardError; end
103
+ class DeliveryError < StandardError; end
84
104
 
85
105
  def self.root
86
106
  Pathname.new(File.expand_path(File.join(__dir__, '..')))
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mailpace-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - MailPace
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-02 00:00:00.000000000 Z
11
+ date: 2024-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionmailer
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 6.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: actionmailbox
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 6.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 6.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: activestorage
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 6.0.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 6.0.0
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: httparty
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -44,14 +72,14 @@ dependencies:
44
72
  requirements:
45
73
  - - ">="
46
74
  - !ruby/object:Gem::Version
47
- version: 6.1.4.1
75
+ version: 7.2.0
48
76
  type: :development
49
77
  prerelease: false
50
78
  version_requirements: !ruby/object:Gem::Requirement
51
79
  requirements:
52
80
  - - ">="
53
81
  - !ruby/object:Gem::Version
54
- version: 6.1.4.1
82
+ version: 7.2.0
55
83
  - !ruby/object:Gem::Dependency
56
84
  name: sqlite3
57
85
  requirement: !ruby/object:Gem::Requirement
@@ -85,7 +113,9 @@ files:
85
113
  homepage: https://mailpace.com
86
114
  licenses:
87
115
  - MIT
88
- metadata: {}
116
+ metadata:
117
+ source_code_uri: https://github.com/mailpace/mailpace-rails
118
+ changelog_uri: https://github.com/mailpace/mailpace-rails/blob/master/CHANGELOG.md
89
119
  post_install_message:
90
120
  rdoc_options: []
91
121
  require_paths:
@@ -101,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
131
  - !ruby/object:Gem::Version
102
132
  version: '0'
103
133
  requirements: []
104
- rubygems_version: 3.3.17
134
+ rubygems_version: 3.5.16
105
135
  signing_key:
106
136
  specification_version: 4
107
137
  summary: Lets you send transactional emails from your app over an easy to use API