mailtrap 2.3.0 → 2.4.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.
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base_api'
4
+ require_relative 'email_template'
5
+
6
+ module Mailtrap
7
+ class EmailTemplatesAPI
8
+ include BaseAPI
9
+
10
+ self.supported_options = %i[name subject category body_html body_text]
11
+
12
+ self.response_class = EmailTemplate
13
+
14
+ # Lists all email templates for the account
15
+ # @return [Array<EmailTemplate>] Array of template objects
16
+ # @!macro api_errors
17
+ def list
18
+ base_list
19
+ end
20
+
21
+ # Retrieves a specific email template
22
+ # @param template_id [Integer] The template ID
23
+ # @return [EmailTemplate] Template object
24
+ # @!macro api_errors
25
+ def get(template_id)
26
+ base_get(template_id)
27
+ end
28
+
29
+ # Creates a new email template
30
+ # @param [Hash] options The parameters to create
31
+ # @option options [String] :name The template name
32
+ # @option options [String] :subject The email subject
33
+ # @option options [String] :category The template category
34
+ # @option options [String, nil] :body_html The HTML content. Default: nil.
35
+ # @option options [String, nil] :body_text The plain text content. Default: nil.
36
+ # @return [EmailTemplate] Created template object
37
+ # @!macro api_errors
38
+ # @raise [ArgumentError] If invalid options are provided
39
+ def create(options)
40
+ base_create(options)
41
+ end
42
+
43
+ # Updates an existing email template
44
+ # @param template_id [Integer] The template ID
45
+ # @param [Hash] options The parameters to update
46
+ # @option options [String] :name The template name
47
+ # @option options [String] :subject The email subject
48
+ # @option options [String] :category The template category
49
+ # @option options [String, nil] :body_html The HTML content
50
+ # @option options [String, nil] :body_text The plain text content
51
+ # @return [EmailTemplate] Updated template object
52
+ # @!macro api_errors
53
+ # @raise [ArgumentError] If invalid options are provided
54
+ def update(template_id, options)
55
+ base_update(template_id, options)
56
+ end
57
+
58
+ # Deletes an email template
59
+ # @param template_id [Integer] The template ID
60
+ # @return nil
61
+ # @!macro api_errors
62
+ def delete(template_id)
63
+ base_delete(template_id)
64
+ end
65
+
66
+ private
67
+
68
+ def base_path
69
+ "/api/accounts/#{account_id}/email_templates"
70
+ end
71
+
72
+ def wrap_request(options)
73
+ { email_template: options }
74
+ end
75
+ end
76
+ end
@@ -5,10 +5,11 @@ require 'json'
5
5
  module Mailtrap
6
6
  module Mail
7
7
  class Base
8
- attr_accessor :from, :to, :reply_to, :cc, :bcc, :headers, :custom_variables, :subject, :text, :html, :category
8
+ attr_accessor :from, :to, :reply_to, :cc, :bcc, :headers, :custom_variables, :subject, :text, :html, :category,
9
+ :template_uuid, :template_variables
9
10
  attr_reader :attachments
10
11
 
11
- def initialize( # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength
12
+ def initialize( # rubocop:disable Metrics/ParameterLists
12
13
  from: nil,
13
14
  to: [],
14
15
  reply_to: nil,
@@ -20,7 +21,9 @@ module Mailtrap
20
21
  attachments: [],
21
22
  headers: {},
22
23
  custom_variables: {},
23
- category: nil
24
+ category: nil,
25
+ template_uuid: nil,
26
+ template_variables: {}
24
27
  )
25
28
  @from = from
26
29
  @to = to
@@ -34,9 +37,11 @@ module Mailtrap
34
37
  @headers = headers
35
38
  @custom_variables = custom_variables
36
39
  @category = category
40
+ @template_uuid = template_uuid
41
+ @template_variables = template_variables
37
42
  end
38
43
 
39
- def as_json # rubocop:disable Metrics/MethodLength
44
+ def as_json
40
45
  {
41
46
  'from' => from,
42
47
  'to' => to,
@@ -50,8 +55,10 @@ module Mailtrap
50
55
  # TODO: update headers and custom_variables with as_json method
51
56
  'headers' => headers,
52
57
  'custom_variables' => custom_variables,
53
- 'category' => category
54
- }.compact
58
+ 'category' => category,
59
+ 'template_uuid' => template_uuid,
60
+ 'template_variables' => template_variables
61
+ }.transform_values { |value| presence value }.compact
55
62
  end
56
63
 
57
64
  def to_json(*args)
@@ -77,6 +84,10 @@ module Mailtrap
77
84
 
78
85
  attachment
79
86
  end
87
+
88
+ def presence(value)
89
+ value.respond_to?(:empty?) && value.empty? ? nil : value
90
+ end
80
91
  end
81
92
  end
82
93
  end
@@ -2,10 +2,9 @@
2
2
 
3
3
  module Mailtrap
4
4
  module Mail
5
+ # @deprecated Use Mailtrap::Mail::Base
5
6
  class FromTemplate < Base
6
- attr_accessor :template_uuid, :template_variables
7
-
8
- def initialize( # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength
7
+ def initialize( # rubocop:disable Metrics/ParameterLists
9
8
  from: nil,
10
9
  to: [],
11
10
  reply_to: nil,
@@ -17,27 +16,7 @@ module Mailtrap
17
16
  template_uuid: nil,
18
17
  template_variables: {}
19
18
  )
20
- super(
21
- from:,
22
- to:,
23
- reply_to:,
24
- cc:,
25
- bcc:,
26
- attachments:,
27
- headers:,
28
- custom_variables:
29
- )
30
- @template_uuid = template_uuid
31
- @template_variables = template_variables
32
- end
33
-
34
- def as_json
35
- super.merge(
36
- {
37
- 'template_uuid' => template_uuid,
38
- 'template_variables' => template_variables
39
- }
40
- ).compact
19
+ super
41
20
  end
42
21
  end
43
22
  end
data/lib/mailtrap/mail.rb CHANGED
@@ -7,11 +7,189 @@ require_relative 'mail/from_template'
7
7
  require_relative 'errors'
8
8
 
9
9
  module Mailtrap
10
- module Mail
10
+ module Mail # rubocop:disable Metrics/ModuleLength
11
+ SPECIAL_HEADERS = %w[
12
+ from
13
+ to
14
+ cc
15
+ bcc
16
+ subject
17
+ category
18
+ customvariables
19
+ contenttype
20
+ ].freeze
21
+ private_constant :SPECIAL_HEADERS
22
+
23
+ # ActionMailer adds these headers by calling `Mail::Message#encoded`,
24
+ # as if the message is to be delivered via SMTP.
25
+ # Since the message will actually be generated on the Mailtrap side from its components,
26
+ # the headers are redundant and potentially conflicting, so we remove them.
27
+ ACTIONMAILER_ADDED_HEADERS = %w[
28
+ contenttransferencoding
29
+ date
30
+ messageid
31
+ mimeversion
32
+ ].freeze
33
+ private_constant :ACTIONMAILER_ADDED_HEADERS
34
+
35
+ HEADERS_TO_REMOVE = (SPECIAL_HEADERS + ACTIONMAILER_ADDED_HEADERS).freeze
36
+ private_constant :HEADERS_TO_REMOVE
37
+
11
38
  class << self
39
+ # Builds a mail object that will be sent using a pre-defined email
40
+ # template. The template content (subject, text, html, category) is
41
+ # defined in the Mailtrap dashboard and referenced by the template_uuid.
42
+ # Template variables can be passed to customize the template content.
43
+ # @example
44
+ # mail = Mailtrap::Mail.from_template(
45
+ # from: { email: 'mailtrap@example.com', name: 'Mailtrap Test' },
46
+ # to: [
47
+ # { email: 'your@email.com' }
48
+ # ],
49
+ # template_uuid: '2f45b0aa-bbed-432f-95e4-e145e1965ba2',
50
+ # template_variables: {
51
+ # 'user_name' => 'John Doe'
52
+ # }
53
+ # )
54
+ def from_template( # rubocop:disable Metrics/ParameterLists
55
+ from: nil,
56
+ to: [],
57
+ reply_to: nil,
58
+ cc: [],
59
+ bcc: [],
60
+ attachments: [],
61
+ headers: {},
62
+ custom_variables: {},
63
+ template_uuid: nil,
64
+ template_variables: {}
65
+ )
66
+ Mailtrap::Mail::Base.new(
67
+ from:,
68
+ to:,
69
+ reply_to:,
70
+ cc:,
71
+ bcc:,
72
+ attachments:,
73
+ headers:,
74
+ custom_variables:,
75
+ template_uuid:,
76
+ template_variables:
77
+ )
78
+ end
79
+
80
+ # Builds a mail object with content including subject, text, html, and category.
81
+ # @example
82
+ # mail = Mailtrap::Mail.from_content(
83
+ # from: { email: 'mailtrap@example.com', name: 'Mailtrap Test' },
84
+ # to: [
85
+ # { email: 'your@email.com' }
86
+ # ],
87
+ # subject: 'You are awesome!',
88
+ # text: 'Congrats for sending test email with Mailtrap!'
89
+ # )
90
+ def from_content( # rubocop:disable Metrics/ParameterLists
91
+ from: nil,
92
+ to: [],
93
+ reply_to: nil,
94
+ cc: [],
95
+ bcc: [],
96
+ attachments: [],
97
+ headers: {},
98
+ custom_variables: {},
99
+ subject: nil,
100
+ text: nil,
101
+ html: nil,
102
+ category: nil
103
+ )
104
+ Mailtrap::Mail::Base.new(
105
+ from:,
106
+ to:,
107
+ reply_to:,
108
+ cc:,
109
+ bcc:,
110
+ attachments:,
111
+ headers:,
112
+ custom_variables:,
113
+ subject:,
114
+ text:,
115
+ html:,
116
+ category:
117
+ )
118
+ end
119
+
120
+ # Builds a base mail object for batch sending using a pre-defined email template.
121
+ # This "base" defines shared properties (such as sender, reply-to, template UUID, and template variables)
122
+ # that will be used as defaults for all emails in the batch. Individual batch requests can override these values.
123
+ # Use this method when you want to send multiple emails with similar content, leveraging a template defined in the Mailtrap dashboard. # rubocop:disable Layout/LineLength
124
+ # Template variables can be passed to customize the template content for all recipients, and can be overridden per request. # rubocop:disable Layout/LineLength
125
+ # @example
126
+ # base_mail = Mailtrap::Mail.batch_base_from_template(
127
+ # from: { email: 'mailtrap@example.com', name: 'Mailtrap Test' },
128
+ # template_uuid: '2f45b0aa-bbed-432f-95e4-e145e1965ba2',
129
+ # template_variables: {
130
+ # 'user_name' => 'John Doe'
131
+ # }
132
+ # )
133
+ # # Use base_mail as the base for batch sending with Mailtrap::Client#send_batch
134
+ def batch_base_from_template( # rubocop:disable Metrics/ParameterLists
135
+ from: nil,
136
+ reply_to: nil,
137
+ attachments: [],
138
+ headers: {},
139
+ custom_variables: {},
140
+ template_uuid: nil,
141
+ template_variables: {}
142
+ )
143
+ Mailtrap::Mail::Base.new(
144
+ from:,
145
+ reply_to:,
146
+ attachments:,
147
+ headers:,
148
+ custom_variables:,
149
+ template_uuid:,
150
+ template_variables:
151
+ )
152
+ end
153
+
154
+ # Builds a base mail object for batch sending with custom content (subject, text, html, category).
155
+ # This "base" defines shared properties for all emails in the batch, such as sender, subject, and body.
156
+ # Individual batch requests can override these values as needed.
157
+ # Use this method when you want to send multiple emails with similar custom content to different recipients.
158
+ # @example
159
+ # base_mail = Mailtrap::Mail.batch_base_from_content(
160
+ # from: { email: 'mailtrap@example.com', name: 'Mailtrap Test' },
161
+ # subject: 'You are awesome!',
162
+ # text: 'Congrats for sending test email with Mailtrap!'
163
+ # )
164
+ # # Use base_mail as the base for batch sending with Mailtrap::Client#send_batch
165
+ def batch_base_from_content( # rubocop:disable Metrics/ParameterLists
166
+ from: nil,
167
+ reply_to: nil,
168
+ attachments: [],
169
+ headers: {},
170
+ custom_variables: {},
171
+ subject: nil,
172
+ text: nil,
173
+ html: nil,
174
+ category: nil
175
+ )
176
+ Mailtrap::Mail::Base.new(
177
+ from:,
178
+ reply_to:,
179
+ attachments:,
180
+ headers:,
181
+ custom_variables:,
182
+ subject:,
183
+ text:,
184
+ html:,
185
+ category:
186
+ )
187
+ end
188
+
189
+ # Builds a mail object from Mail::Message
12
190
  # @param message [Mail::Message]
13
191
  # @return [Mailtrap::Mail::Base]
14
- def from_message(message) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
192
+ def from_message(message) # rubocop:disable Metrics/AbcSize
15
193
  Mailtrap::Mail::Base.new(
16
194
  from: prepare_addresses(address_list(message['from'])&.addresses).first,
17
195
  to: prepare_addresses(address_list(message['to'])&.addresses),
@@ -29,30 +207,6 @@ module Mailtrap
29
207
 
30
208
  private
31
209
 
32
- SPECIAL_HEADERS = %w[
33
- from
34
- to
35
- cc
36
- bcc
37
- subject
38
- category
39
- customvariables
40
- contenttype
41
- ].freeze
42
-
43
- # ActionMailer adds these headers by calling `Mail::Message#encoded`,
44
- # as if the message is to be delivered via SMTP.
45
- # Since the message will actually be generated on the Mailtrap side from its components,
46
- # the headers are redundant and potentially conflicting, so we remove them.
47
- ACTIONMAILER_ADDED_HEADERS = %w[
48
- contenttransferencoding
49
- date
50
- messageid
51
- mimeversion
52
- ].freeze
53
-
54
- HEADERS_TO_REMOVE = (SPECIAL_HEADERS + ACTIONMAILER_ADDED_HEADERS).freeze
55
-
56
210
  # @param header [Mail::Field, nil]
57
211
  # @return [Mail::AddressList, nil]
58
212
  def address_list(header)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mailtrap
4
- VERSION = '2.3.0'
4
+ VERSION = '2.4.0'
5
5
  end
data/lib/mailtrap.rb CHANGED
@@ -4,5 +4,15 @@ require_relative 'mailtrap/action_mailer' if defined? ActionMailer
4
4
  require_relative 'mailtrap/mail'
5
5
  require_relative 'mailtrap/errors'
6
6
  require_relative 'mailtrap/version'
7
+ require_relative 'mailtrap/email_templates_api'
8
+ require_relative 'mailtrap/contacts_api'
9
+ require_relative 'mailtrap/contact_lists_api'
10
+ require_relative 'mailtrap/contact_fields_api'
7
11
 
8
- module Mailtrap; end
12
+ module Mailtrap
13
+ # @!macro api_errors
14
+ # @raise [Mailtrap::Error] If the API request fails with a client or server error
15
+ # @raise [Mailtrap::AuthorizationError] If the API key is invalid
16
+ # @raise [Mailtrap::RejectionError] If the server refuses to process the request
17
+ # @raise [Mailtrap::RateLimitError] If too many requests are made
18
+ end
data/mailtrap.gemspec CHANGED
@@ -19,6 +19,8 @@ Gem::Specification.new do |spec|
19
19
  spec.metadata['changelog_uri'] = 'https://github.com/railsware/mailtrap-ruby/blob/main/CHANGELOG.md'
20
20
  spec.metadata['rubygems_mfa_required'] = 'true'
21
21
 
22
+ spec.add_dependency 'base64'
23
+
22
24
  # Specify which files should be added to the gem when it is released.
23
25
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
26
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mailtrap
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 2.4.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: 2025-03-07 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2025-08-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: base64
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: Official mailtrap.io API client
14
28
  email:
15
29
  - support@mailtrap.io
@@ -32,7 +46,16 @@ files:
32
46
  - lib/mailtrap/action_mailer/delivery_method.rb
33
47
  - lib/mailtrap/action_mailer/railtie.rb
34
48
  - lib/mailtrap/attachment.rb
49
+ - lib/mailtrap/base_api.rb
35
50
  - lib/mailtrap/client.rb
51
+ - lib/mailtrap/contact.rb
52
+ - lib/mailtrap/contact_field.rb
53
+ - lib/mailtrap/contact_fields_api.rb
54
+ - lib/mailtrap/contact_list.rb
55
+ - lib/mailtrap/contact_lists_api.rb
56
+ - lib/mailtrap/contacts_api.rb
57
+ - lib/mailtrap/email_template.rb
58
+ - lib/mailtrap/email_templates_api.rb
36
59
  - lib/mailtrap/errors.rb
37
60
  - lib/mailtrap/mail.rb
38
61
  - lib/mailtrap/mail/base.rb