mailtrap 1.2.2 → 2.1.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: c02915dc1ea123fb592af56b9c553cb97e6137f0555b98f965f3a8cf1dd1d229
4
- data.tar.gz: 8e7da7be2313f3e3766512e5c4014f90d1f8f1ddb271cbd255494252b6eda161
3
+ metadata.gz: 5423fd6ba2817ace06e724da478e925615f3c8b4dfcbf24eb271f6bb12fded4c
4
+ data.tar.gz: b331be242b93d7176adf0ca92d01f33d0bc7a9407766d8d139d8bb39d7d2382b
5
5
  SHA512:
6
- metadata.gz: 962e1a2227a79b264bd637f643ffbf326c03d56ba1736a406869fda693eb67c5ffed3797cacd1862403e034ba543c44094829fe45ec36c6fcfa2e96ecdd554c3
7
- data.tar.gz: b568ff9976cba7311ae02118bbbe5b825230743facd0094921fa3ac58d4fe4d8132c9b9bbc5dca4c6d60fc798fa18d8508149a871949de971e621af50c386177
6
+ metadata.gz: 9c2556e7ea3b7405ec76910a718ba949694563dabc4547fc6b33771bd52a2bd0e444e287bcc341c6c5e1bdab07bd12573c3ce9607cbcc84afbd8398aafbccf4c
7
+ data.tar.gz: 06f0ea3e473461ad889e101540ba2d2a37cfda75ce3a4677ab58708ddae8ce4cae53d16426f4b30e54bf899e9a5f3cca0c46e0616c49b9da80be9c1e8c308c67
data/.rubocop.yml CHANGED
@@ -1,10 +1,16 @@
1
- require: rubocop-rspec
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+
5
+ inherit_mode:
6
+ merge:
7
+ - Exclude
2
8
 
3
9
  AllCops:
4
- TargetRubyVersion: 2.7
10
+ TargetRubyVersion: 3.0
5
11
  NewCops: enable
6
12
  Exclude:
7
- - 'gemfiles/**/*'
13
+ - "gemfiles/**/*"
8
14
 
9
15
  Layout/LineLength:
10
16
  Max: 120
@@ -31,3 +37,11 @@ Style/StringLiterals:
31
37
  Style/StringLiteralsInInterpolation:
32
38
  Enabled: true
33
39
  EnforcedStyle: double_quotes
40
+
41
+ Style/FrozenStringLiteralComment:
42
+ Exclude:
43
+ - "examples/**/*"
44
+
45
+ Style/TrailingCommaInHashLiteral:
46
+ Exclude:
47
+ - "examples/**/*"
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.7.6
1
+ 3.3.3
data/CHANGELOG.md CHANGED
@@ -1,3 +1,24 @@
1
+ ## [2.1.0] - 2024-07-08
2
+
3
+ - Fixed extraneous headers added by ActionMailer #21
4
+ - Dropped Ruby 2.7 support and added test coverage for Ruby up to 3.3 #22
5
+
6
+ ## [2.0.0] - 2024-03-20
7
+
8
+ - Added arguments for `Mailtrap::Client`
9
+ - `bulk` to use Mailtrap bulk sending API
10
+ - `sandbox` to use Mailtrap sandbox API
11
+ - `inbox_id` required when using Mailtrap sandbox API
12
+
13
+ - Removed Sending namespace, affected classes:
14
+ - `Mailtrap::Sending::Client` -> `Mailtrap::Client`
15
+ - `Mailtrap::Sending::Error` -> `Mailtrap::Error`
16
+ - `Mailtrap::Sending::AttachmentContentError` -> `Mailtrap::AttachmentContentError`
17
+ - `Mailtrap::Sending::AuthorizationError` -> `Mailtrap::AuthorizationError`
18
+ - `Mailtrap::Sending::MailSizeError` -> `Mailtrap::MailSizeError`
19
+ - `Mailtrap::Sending::RateLimitError` -> `Mailtrap::RateLimitError`
20
+ - `Mailtrap::Sending::RejectionError` -> `Mailtrap::RejectionError`
21
+
1
22
  ## [1.2.2] - 2023-11-01
2
23
 
3
24
  - Improved error handling
data/Gemfile CHANGED
@@ -11,6 +11,7 @@ gem 'rake', '~> 13.0'
11
11
  gem 'rspec', '~> 3.0'
12
12
  gem 'rspec-its'
13
13
  gem 'rubocop', '~> 1.21'
14
+ gem 'rubocop-rake', require: false
14
15
  gem 'rubocop-rspec', require: false
15
16
  gem 'vcr'
16
17
  gem 'webmock'
data/Gemfile.lock CHANGED
@@ -1,68 +1,93 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mailtrap (1.2.2)
4
+ mailtrap (2.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- addressable (2.8.0)
10
- public_suffix (>= 2.0.2, < 5.0)
11
- appraisal (2.4.1)
9
+ addressable (2.8.7)
10
+ public_suffix (>= 2.0.2, < 7.0)
11
+ appraisal (2.5.0)
12
12
  bundler
13
13
  rake
14
14
  thor (>= 0.14.0)
15
15
  ast (2.4.2)
16
- crack (0.4.5)
16
+ bigdecimal (3.1.8)
17
+ crack (1.0.0)
18
+ bigdecimal
17
19
  rexml
18
- diff-lcs (1.5.0)
19
- hashdiff (1.0.1)
20
- mail (2.7.1)
20
+ date (3.3.4)
21
+ diff-lcs (1.5.1)
22
+ hashdiff (1.1.0)
23
+ json (2.7.2)
24
+ language_server-protocol (3.17.0.3)
25
+ mail (2.8.1)
21
26
  mini_mime (>= 0.1.1)
22
- mini_mime (1.1.2)
23
- net-smtp (0.1.0)
24
- parallel (1.22.1)
25
- parser (3.1.2.0)
27
+ net-imap
28
+ net-pop
29
+ net-smtp
30
+ mini_mime (1.1.5)
31
+ net-imap (0.4.14)
32
+ date
33
+ net-protocol
34
+ net-pop (0.1.2)
35
+ net-protocol
36
+ net-protocol (0.2.2)
37
+ timeout
38
+ net-smtp (0.5.0)
39
+ net-protocol
40
+ parallel (1.25.1)
41
+ parser (3.3.3.0)
26
42
  ast (~> 2.4.1)
27
- public_suffix (4.0.7)
43
+ racc
44
+ public_suffix (5.1.1)
45
+ racc (1.8.0)
28
46
  rainbow (3.1.1)
29
- rake (13.0.6)
30
- regexp_parser (2.4.0)
31
- rexml (3.2.5)
32
- rspec (3.11.0)
33
- rspec-core (~> 3.11.0)
34
- rspec-expectations (~> 3.11.0)
35
- rspec-mocks (~> 3.11.0)
36
- rspec-core (3.11.0)
37
- rspec-support (~> 3.11.0)
38
- rspec-expectations (3.11.0)
47
+ rake (13.2.1)
48
+ regexp_parser (2.9.2)
49
+ rexml (3.3.1)
50
+ strscan
51
+ rspec (3.13.0)
52
+ rspec-core (~> 3.13.0)
53
+ rspec-expectations (~> 3.13.0)
54
+ rspec-mocks (~> 3.13.0)
55
+ rspec-core (3.13.0)
56
+ rspec-support (~> 3.13.0)
57
+ rspec-expectations (3.13.1)
39
58
  diff-lcs (>= 1.2.0, < 2.0)
40
- rspec-support (~> 3.11.0)
59
+ rspec-support (~> 3.13.0)
41
60
  rspec-its (1.3.0)
42
61
  rspec-core (>= 3.0.0)
43
62
  rspec-expectations (>= 3.0.0)
44
- rspec-mocks (3.11.1)
63
+ rspec-mocks (3.13.1)
45
64
  diff-lcs (>= 1.2.0, < 2.0)
46
- rspec-support (~> 3.11.0)
47
- rspec-support (3.11.0)
48
- rubocop (1.29.1)
65
+ rspec-support (~> 3.13.0)
66
+ rspec-support (3.13.1)
67
+ rubocop (1.64.1)
68
+ json (~> 2.3)
69
+ language_server-protocol (>= 3.17.0)
49
70
  parallel (~> 1.10)
50
- parser (>= 3.1.0.0)
71
+ parser (>= 3.3.0.2)
51
72
  rainbow (>= 2.2.2, < 4.0)
52
73
  regexp_parser (>= 1.8, < 3.0)
53
74
  rexml (>= 3.2.5, < 4.0)
54
- rubocop-ast (>= 1.17.0, < 2.0)
75
+ rubocop-ast (>= 1.31.1, < 2.0)
55
76
  ruby-progressbar (~> 1.7)
56
- unicode-display_width (>= 1.4.0, < 3.0)
57
- rubocop-ast (1.18.0)
58
- parser (>= 3.1.1.0)
59
- rubocop-rspec (2.11.1)
60
- rubocop (~> 1.19)
61
- ruby-progressbar (1.11.0)
62
- thor (1.2.1)
63
- unicode-display_width (2.1.0)
64
- vcr (6.1.0)
65
- webmock (3.14.0)
77
+ unicode-display_width (>= 2.4.0, < 3.0)
78
+ rubocop-ast (1.31.3)
79
+ parser (>= 3.3.1.0)
80
+ rubocop-rake (0.6.0)
81
+ rubocop (~> 1.0)
82
+ rubocop-rspec (3.0.2)
83
+ rubocop (~> 1.61)
84
+ ruby-progressbar (1.13.0)
85
+ strscan (3.1.0)
86
+ thor (1.3.1)
87
+ timeout (0.4.1)
88
+ unicode-display_width (2.5.0)
89
+ vcr (6.2.0)
90
+ webmock (3.23.1)
66
91
  addressable (>= 2.8.0)
67
92
  crack (>= 0.3.2)
68
93
  hashdiff (>= 0.4.0, < 2.0.0)
@@ -79,9 +104,10 @@ DEPENDENCIES
79
104
  rspec (~> 3.0)
80
105
  rspec-its
81
106
  rubocop (~> 1.21)
107
+ rubocop-rake
82
108
  rubocop-rspec
83
109
  vcr
84
110
  webmock
85
111
 
86
112
  BUNDLED WITH
87
- 2.1.4
113
+ 2.5.14
data/README.md CHANGED
@@ -6,6 +6,8 @@ This Ruby gem offers integration with the [official API](https://api-docs.mailtr
6
6
 
7
7
  Quickly add email sending functionality to your Ruby application with Mailtrap.
8
8
 
9
+ (This client uses API v2, for v1 refer to [this documentation](https://mailtrap.docs.apiary.io/))
10
+
9
11
  ## Installation
10
12
 
11
13
  Add this line to your application's Gemfile:
@@ -40,97 +42,17 @@ mail = Mailtrap::Mail::Base.new(
40
42
  )
41
43
 
42
44
  # create client and send
43
- client = Mailtrap::Sending::Client.new(api_key: 'your-api-key')
45
+ client = Mailtrap::Client.new(api_key: 'your-api-key')
44
46
  client.send(mail)
45
47
  ```
46
48
 
47
- ### Full
49
+ Refer to the [`examples`](examples) folder for other examples.
48
50
 
49
- ```ruby
50
- require 'mailtrap'
51
- require 'base64'
51
+ - [Full](examples/full.rb)
52
+ - [Email template](examples/email_template.rb)
53
+ - [ActionMailer](examples/action_mailer.rb)
52
54
 
53
- mail = Mailtrap::Mail::Base.new(
54
- from: { email: 'mailtrap@example.com', name: 'Mailtrap Test' },
55
- to: [
56
- { email: 'your@email.com', name: 'Your name' }
57
- ],
58
- cc: [
59
- { email: 'cc@email.com', name: 'Copy To' }
60
- ],
61
- bcc: [
62
- { email: 'bcc@email.com', name: 'Hidden Recipient' }
63
- ],
64
- subject: 'You are awesome!',
65
- text: "Congrats for sending test email with Mailtrap!",
66
- category: "Integration Test",
67
- attachments: [
68
- {
69
- content: Base64.encode64('Attachment content'), # base64 encoded content or IO string
70
- filename: 'attachment.txt'
71
- }
72
- ],
73
- headers: {
74
- 'X-MT-Header': 'Custom header'
75
- },
76
- custom_variables: {
77
- year: 2022
78
- }
79
- )
80
-
81
- data = File.open('/path/to/image.jpg').read
82
- encoded = Base64.encode64(data).gsub(/\n/,"")
83
-
84
- mail.add_attachment(content: encoded, filename: 'image.png')
85
-
86
- client = Mailtrap::Sending::Client.new(api_key: 'your-api-key')
87
- client.send(mail)
88
- ```
89
-
90
- ### Using email template
91
-
92
- ```ruby
93
- require 'mailtrap'
94
-
95
- # create mail object
96
- mail = Mailtrap::Mail::FromTemplate.new(
97
- from: { email: 'mailtrap@example.com', name: 'Mailtrap Test' },
98
- to: [
99
- { email: 'your@email.com' }
100
- ],
101
- template_uuid: '2f45b0aa-bbed-432f-95e4-e145e1965ba2',
102
- template_variables: {
103
- 'user_name' => 'John Doe'
104
- }
105
- )
106
-
107
- # create client and send
108
- client = Mailtrap::Sending::Client.new(api_key: 'your-api-key')
109
- client.send(mail)
110
- ```
111
-
112
- ### ActionMailer
113
-
114
- This gem also adds ActionMailer delivery method. To configure it, add following to your ActionMailer configuration (in Rails projects located in `config/$ENVIRONMENT.rb`):
115
- ```ruby
116
- config.action_mailer.delivery_method = :mailtrap
117
- config.action_mailer.mailtrap_settings = {
118
- api_key: ENV.fetch('MAILTRAP_API_KEY')
119
- }
120
- ```
121
- And continue to use ActionMailer as usual.
122
-
123
- To add `category` and `custom_variables`, add them to the mail generation:
124
- ```ruby
125
- mail(
126
- to: 'your@email.com',
127
- subject: 'You are awesome!',
128
- category: 'Test category',
129
- custom_variables: { test_variable: 'abc' }
130
- )
131
- ```
132
-
133
- #### Content-Transfer-Encoding
55
+ ### Content-Transfer-Encoding
134
56
 
135
57
  `mailtrap` gem uses Mailtrap API to send emails. Mailtrap API does not try to
136
58
  replicate SMTP. That is why you should expect some limitations when it comes to
@@ -144,6 +66,12 @@ better flexibility in that regard. Go to your _Mailtrap account_ → _Email Send
144
66
  → _Sending Domains_ → _Your domain_ → _SMTP/API Settings_ to find the SMTP
145
67
  configuration example.
146
68
 
69
+ ## Migration guide v1 → v2
70
+
71
+ Change `Mailtrap::Sending::Client` to `Mailtrap::Client`.
72
+
73
+ If you use classes which have `Sending` namespace, remove the namespace like in the example above.
74
+
147
75
  ## Development
148
76
 
149
77
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -5,6 +5,8 @@ module Mailtrap
5
5
  class DeliveryMethod
6
6
  attr_accessor :settings
7
7
 
8
+ ALLOWED_PARAMS = %i[api_key api_host api_port bulk sandbox inbox_id].freeze
9
+
8
10
  def initialize(settings)
9
11
  self.settings = settings
10
12
  end
@@ -18,7 +20,7 @@ module Mailtrap
18
20
  private
19
21
 
20
22
  def client
21
- @client ||= Mailtrap::Sending::Client.new(**settings.slice(:api_key, :api_host, :api_port))
23
+ @client ||= Mailtrap::Client.new(**settings.slice(*ALLOWED_PARAMS))
22
24
  end
23
25
  end
24
26
  end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'base64'
4
+ require 'json'
5
+
6
+ module Mailtrap
7
+ class Attachment
8
+ attr_accessor :type, :filename, :disposition, :content_id
9
+ attr_reader :content
10
+
11
+ def initialize(content:, filename:, type: nil, disposition: nil, content_id: nil)
12
+ self.content = content
13
+ @type = type
14
+ @filename = filename
15
+ @disposition = disposition
16
+ @content_id = content_id
17
+ end
18
+
19
+ def as_json
20
+ {
21
+ 'content' => content,
22
+ 'type' => type,
23
+ 'filename' => filename,
24
+ 'disposition' => disposition,
25
+ 'content_id' => content_id
26
+ }.compact
27
+ end
28
+
29
+ def content=(content)
30
+ if content.respond_to?(:read)
31
+ @content = encode(content)
32
+ else
33
+ raise AttachmentContentError unless base64?(content)
34
+
35
+ @content = content
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def encode(io)
42
+ string = io.read.encode('UTF-8') unless io.respond_to?(:binmode?) && io.binmode?
43
+ Base64.encode64(string).gsub("\n", '')
44
+ end
45
+
46
+ def base64?(string)
47
+ string.is_a?(String) && Base64.strict_encode64(Base64.decode64(string)) == string
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'net/http'
5
+ require 'uri'
6
+
7
+ module Mailtrap
8
+ class Client
9
+ SENDING_API_HOST = 'send.api.mailtrap.io'
10
+ BULK_SENDING_API_HOST = 'bulk.api.mailtrap.io'
11
+ SANDBOX_API_HOST = 'sandbox.api.mailtrap.io'
12
+ API_PORT = 443
13
+
14
+ attr_reader :api_key, :api_host, :api_port, :bulk, :sandbox, :inbox_id
15
+
16
+ # Initializes a new Mailtrap::Client instance.
17
+ #
18
+ # @param [String] api_key The Mailtrap API key to use for sending. Required.
19
+ # If not set, is taken from the MAILTRAP_API_KEY environment variable.
20
+ # @param [String, nil] api_host The Mailtrap API hostname. If not set, is chosen internally.
21
+ # @param [Integer] api_port The Mailtrap API port. Default: 443.
22
+ # @param [Boolean] bulk Whether to use the Mailtrap bulk sending API. Default: false.
23
+ # If enabled, is incompatible with `sandbox: true`.
24
+ # @param [Boolean] sandbox Whether to use the Mailtrap sandbox API. Default: false.
25
+ # If enabled, is incompatible with `bulk: true`.
26
+ # @param [Integer] inbox_id The sandbox inbox ID to send to. Required if sandbox API is used.
27
+ def initialize( # rubocop:disable Metrics/ParameterLists
28
+ api_key: ENV.fetch('MAILTRAP_API_KEY'),
29
+ api_host: nil,
30
+ api_port: API_PORT,
31
+ bulk: false,
32
+ sandbox: false,
33
+ inbox_id: nil
34
+ )
35
+ raise ArgumentError, 'api_key is required' if api_key.nil?
36
+ raise ArgumentError, 'api_port is required' if api_port.nil?
37
+
38
+ api_host ||= select_api_host(bulk: bulk, sandbox: sandbox)
39
+ raise ArgumentError, 'inbox_id is required for sandbox API' if sandbox && inbox_id.nil?
40
+
41
+ @api_key = api_key
42
+ @api_host = api_host
43
+ @api_port = api_port
44
+ @bulk = bulk
45
+ @sandbox = sandbox
46
+ @inbox_id = inbox_id
47
+ end
48
+
49
+ def send(mail)
50
+ raise ArgumentError, 'should be Mailtrap::Mail::Base object' unless mail.is_a? Mail::Base
51
+
52
+ request = post_request(request_url, mail.to_json)
53
+ response = http_client.request(request)
54
+
55
+ handle_response(response)
56
+ end
57
+
58
+ private
59
+
60
+ def select_api_host(bulk:, sandbox:)
61
+ raise ArgumentError, 'bulk mode is not applicable for sandbox API' if bulk && sandbox
62
+
63
+ if sandbox
64
+ SANDBOX_API_HOST
65
+ elsif bulk
66
+ BULK_SENDING_API_HOST
67
+ else
68
+ SENDING_API_HOST
69
+ end
70
+ end
71
+
72
+ def request_url
73
+ "/api/send#{sandbox ? "/#{inbox_id}" : ""}"
74
+ end
75
+
76
+ def http_client
77
+ @http_client ||= Net::HTTP.new(api_host, api_port).tap { |client| client.use_ssl = true }
78
+ end
79
+
80
+ def post_request(path, body)
81
+ request = Net::HTTP::Post.new(path)
82
+ request.body = body
83
+ request['Authorization'] = "Bearer #{api_key}"
84
+ request['Content-Type'] = 'application/json'
85
+ request['User-Agent'] = 'mailtrap-ruby (https://github.com/railsware/mailtrap-ruby)'
86
+
87
+ request
88
+ end
89
+
90
+ def handle_response(response) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
91
+ case response
92
+ when Net::HTTPOK
93
+ json_response(response.body)
94
+ when Net::HTTPBadRequest
95
+ raise Mailtrap::Error, json_response(response.body)[:errors]
96
+ when Net::HTTPUnauthorized
97
+ raise Mailtrap::AuthorizationError, json_response(response.body)[:errors]
98
+ when Net::HTTPForbidden
99
+ raise Mailtrap::RejectionError, json_response(response.body)[:errors]
100
+ when Net::HTTPPayloadTooLarge
101
+ raise Mailtrap::MailSizeError, ['message too large']
102
+ when Net::HTTPTooManyRequests
103
+ raise Mailtrap::RateLimitError, ['too many requests']
104
+ when Net::HTTPClientError
105
+ raise Mailtrap::Error, ['client error']
106
+ when Net::HTTPServerError
107
+ raise Mailtrap::Error, ['server error']
108
+ else
109
+ raise Mailtrap::Error, ["unexpected status code=#{response.code}"]
110
+ end
111
+ end
112
+
113
+ def json_response(body)
114
+ JSON.parse(body, symbolize_names: true)
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'attachment'
4
+ require_relative 'client'
5
+
6
+ module Mailtrap
7
+ class AttachmentContentError < StandardError; end
8
+
9
+ class Error < StandardError
10
+ attr_reader :messages
11
+
12
+ def initialize(messages)
13
+ @messages = messages
14
+
15
+ super(messages.join(', '))
16
+ end
17
+ end
18
+
19
+ # AuthorizationError is raised when invalid token is used.
20
+ class AuthorizationError < Error; end
21
+
22
+ # MailSizeError is raised when mail is too large.
23
+ class MailSizeError < Error; end
24
+
25
+ # RateLimitError is raised when client performing too many requests.
26
+ class RateLimitError < Error; end
27
+
28
+ # RejectionError is raised when server refuses to process the request. Use
29
+ # error message to debug the problem.
30
+ #
31
+ # *Some* possible reasons:
32
+ # * Account is banned
33
+ # * Domain is not verified
34
+ class RejectionError < Error; end
35
+ end
@@ -59,11 +59,11 @@ module Mailtrap
59
59
  end
60
60
 
61
61
  def attachments=(attachments)
62
- @attachments = attachments.map { |attachment| Mailtrap::Sending::Attachment.new(**attachment) }
62
+ @attachments = attachments.map { |attachment| Mailtrap::Attachment.new(**attachment) }
63
63
  end
64
64
 
65
65
  def add_attachment(content:, filename:, type: nil, disposition: nil, content_id: nil)
66
- attachment = Mailtrap::Sending::Attachment.new(
66
+ attachment = Mailtrap::Attachment.new(
67
67
  content: content,
68
68
  filename: filename,
69
69
  type: type,
data/lib/mailtrap/mail.rb CHANGED
@@ -26,7 +26,7 @@ module Mailtrap
26
26
 
27
27
  private
28
28
 
29
- PROCESSED_HEADERS = %w[
29
+ SPECIAL_HEADERS = %w[
30
30
  from
31
31
  to
32
32
  cc
@@ -37,6 +37,19 @@ module Mailtrap
37
37
  contenttype
38
38
  ].freeze
39
39
 
40
+ # ActionMailer adds these headers by calling `Mail::Message#encoded`,
41
+ # as if the message is to be delivered via SMTP.
42
+ # Since the message will actually be generated on the Mailtrap side from its components,
43
+ # the headers are redundant and potentially conflicting, so we remove them.
44
+ ACTIONMAILER_ADDED_HEADERS = %w[
45
+ contenttransferencoding
46
+ date
47
+ messageid
48
+ mimeversion
49
+ ].freeze
50
+
51
+ HEADERS_TO_REMOVE = (SPECIAL_HEADERS + ACTIONMAILER_ADDED_HEADERS).freeze
52
+
40
53
  def address_list(header)
41
54
  header.respond_to?(:element) ? header.element : header&.address_list
42
55
  end
@@ -48,7 +61,7 @@ module Mailtrap
48
61
  def prepare_headers(message)
49
62
  message
50
63
  .header_fields
51
- .reject { |header| PROCESSED_HEADERS.include?(header.name.downcase.delete('-')) }
64
+ .reject { |header| HEADERS_TO_REMOVE.include?(header.name.downcase.delete('-')) }
52
65
  .to_h { |header| [header.name, header.value] }
53
66
  .compact
54
67
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mailtrap
4
- VERSION = '1.2.2'
4
+ VERSION = '2.1.0'
5
5
  end
data/lib/mailtrap.rb CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  require_relative 'mailtrap/action_mailer' if defined? ActionMailer
4
4
  require_relative 'mailtrap/mail'
5
- require_relative 'mailtrap/sending'
5
+ require_relative 'mailtrap/errors'
6
6
  require_relative 'mailtrap/version'
7
7
 
8
8
  module Mailtrap; end
data/mailtrap.gemspec CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
  spec.description = 'Official mailtrap.io API client'
13
13
  spec.homepage = 'https://github.com/railsware/mailtrap-ruby'
14
14
  spec.license = 'MIT'
15
- spec.required_ruby_version = '>= 2.7.0'
15
+ spec.required_ruby_version = '>= 3.0.0'
16
16
 
17
17
  spec.metadata['homepage_uri'] = spec.homepage
18
18
  spec.metadata['source_code_uri'] = 'https://github.com/railsware/mailtrap-ruby'
@@ -23,7 +23,9 @@ Gem::Specification.new do |spec|
23
23
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
24
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
25
  `git ls-files -z`.split("\x0").reject do |f|
26
- (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|github|travis|circleci)|appveyor)})
26
+ (f == __FILE__) || f.match(
27
+ %r{\A(?:(?:bin|test|spec|features|examples)/|\.(?:git|github|travis|circleci)|appveyor)}
28
+ )
27
29
  end
28
30
  end
29
31
  spec.require_paths = ['lib']
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mailtrap
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Railsware Products Studio LLC
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-09 00:00:00.000000000 Z
11
+ date: 2024-07-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Official mailtrap.io API client
14
14
  email:
@@ -32,12 +32,12 @@ files:
32
32
  - lib/mailtrap/action_mailer.rb
33
33
  - lib/mailtrap/action_mailer/delivery_method.rb
34
34
  - lib/mailtrap/action_mailer/railtie.rb
35
+ - lib/mailtrap/attachment.rb
36
+ - lib/mailtrap/client.rb
37
+ - lib/mailtrap/errors.rb
35
38
  - lib/mailtrap/mail.rb
36
39
  - lib/mailtrap/mail/base.rb
37
40
  - lib/mailtrap/mail/from_template.rb
38
- - lib/mailtrap/sending.rb
39
- - lib/mailtrap/sending/attachment.rb
40
- - lib/mailtrap/sending/client.rb
41
41
  - lib/mailtrap/version.rb
42
42
  - mailtrap.gemspec
43
43
  homepage: https://github.com/railsware/mailtrap-ruby
@@ -56,14 +56,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
56
56
  requirements:
57
57
  - - ">="
58
58
  - !ruby/object:Gem::Version
59
- version: 2.7.0
59
+ version: 3.0.0
60
60
  required_rubygems_version: !ruby/object:Gem::Requirement
61
61
  requirements:
62
62
  - - ">="
63
63
  - !ruby/object:Gem::Version
64
64
  version: '0'
65
65
  requirements: []
66
- rubygems_version: 3.1.6
66
+ rubygems_version: 3.5.11
67
67
  signing_key:
68
68
  specification_version: 4
69
69
  summary: Official mailtrap.io API client
@@ -1,52 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'base64'
4
- require 'json'
5
-
6
- module Mailtrap
7
- module Sending
8
- class Attachment
9
- attr_accessor :type, :filename, :disposition, :content_id
10
- attr_reader :content
11
-
12
- def initialize(content:, filename:, type: nil, disposition: nil, content_id: nil)
13
- self.content = content
14
- @type = type
15
- @filename = filename
16
- @disposition = disposition
17
- @content_id = content_id
18
- end
19
-
20
- def as_json
21
- {
22
- 'content' => content,
23
- 'type' => type,
24
- 'filename' => filename,
25
- 'disposition' => disposition,
26
- 'content_id' => content_id
27
- }.compact
28
- end
29
-
30
- def content=(content)
31
- if content.respond_to?(:read)
32
- @content = encode(content)
33
- else
34
- raise AttachmentContentError unless base64?(content)
35
-
36
- @content = content
37
- end
38
- end
39
-
40
- private
41
-
42
- def encode(io)
43
- string = io.read.encode('UTF-8') unless io.respond_to?(:binmode?) && io.binmode?
44
- Base64.encode64(string).gsub(/\n/, '')
45
- end
46
-
47
- def base64?(string)
48
- string.is_a?(String) && Base64.strict_encode64(Base64.decode64(string)) == string
49
- end
50
- end
51
- end
52
- end
@@ -1,74 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'json'
4
- require 'net/http'
5
- require 'uri'
6
-
7
- module Mailtrap
8
- module Sending
9
- class Client
10
- API_HOST = 'send.api.mailtrap.io'
11
- API_PORT = 443
12
-
13
- attr_reader :api_key, :api_host, :api_port
14
-
15
- def initialize(api_key: ENV.fetch('MAILTRAP_API_KEY'), api_host: API_HOST, api_port: API_PORT)
16
- @api_key = api_key
17
- @api_host = api_host
18
- @api_port = api_port
19
- end
20
-
21
- def send(mail)
22
- raise ArgumentError, 'should be Mailtrap::Mail::Base object' unless mail.is_a? Mail::Base
23
-
24
- request = post_request('/api/send', mail.to_json)
25
- response = http_client.request(request)
26
-
27
- handle_response(response)
28
- end
29
-
30
- private
31
-
32
- def http_client
33
- @http_client ||= Net::HTTP.new(api_host, api_port).tap { |client| client.use_ssl = true }
34
- end
35
-
36
- def post_request(path, body)
37
- request = Net::HTTP::Post.new(path)
38
- request.body = body
39
- request['Authorization'] = "Bearer #{api_key}"
40
- request['Content-Type'] = 'application/json'
41
- request['User-Agent'] = 'mailtrap-ruby (https://github.com/railsware/mailtrap-ruby)'
42
-
43
- request
44
- end
45
-
46
- def handle_response(response) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
47
- case response
48
- when Net::HTTPOK
49
- json_response(response.body)
50
- when Net::HTTPBadRequest
51
- raise Mailtrap::Sending::Error, json_response(response.body)[:errors]
52
- when Net::HTTPUnauthorized
53
- raise Mailtrap::Sending::AuthorizationError, json_response(response.body)[:errors]
54
- when Net::HTTPForbidden
55
- raise Mailtrap::Sending::RejectionError, json_response(response.body)[:errors]
56
- when Net::HTTPPayloadTooLarge
57
- raise Mailtrap::Sending::MailSizeError, ['message too large']
58
- when Net::HTTPTooManyRequests
59
- raise Mailtrap::Sending::RateLimitError, ['too many requests']
60
- when Net::HTTPClientError
61
- raise Mailtrap::Sending::Error, ['client error']
62
- when Net::HTTPServerError
63
- raise Mailtrap::Sending::Error, ['server error']
64
- else
65
- raise Mailtrap::Sending::Error, ["unexpected status code=#{response.code}"]
66
- end
67
- end
68
-
69
- def json_response(body)
70
- JSON.parse(body, symbolize_names: true)
71
- end
72
- end
73
- end
74
- end
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'sending/attachment'
4
- require_relative 'sending/client'
5
-
6
- module Mailtrap
7
- module Sending
8
- class AttachmentContentError < StandardError; end
9
-
10
- class Error < StandardError
11
- attr_reader :messages
12
-
13
- def initialize(messages)
14
- @messages = messages
15
-
16
- super(messages.join(', '))
17
- end
18
- end
19
-
20
- # AuthorizationError is raised when invalid token is used.
21
- class AuthorizationError < Error; end
22
-
23
- # MailSizeError is raised when mail is too large.
24
- class MailSizeError < Error; end
25
-
26
- # RateLimitError is raised when client performing too many requests.
27
- class RateLimitError < Error; end
28
-
29
- # RejectionError is raised when server refuses to process the request. Use
30
- # error message to debug the problem.
31
- #
32
- # *Some* possible reasons:
33
- # * Account is banned
34
- # * Domain is not verified
35
- class RejectionError < Error; end
36
- end
37
- end