Skip to content

Commit f93bbd3

Browse files
committed
added Mail::Batch classes, updated batch examples
1 parent 183af30 commit f93bbd3

File tree

12 files changed

+275
-51
lines changed

12 files changed

+275
-51
lines changed

examples/batch.rb

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
require 'bundler/setup'
21
require 'mailtrap'
32
require 'base64'
43

5-
mail = Mailtrap::Mail::Base.new(
6-
from: { email: '[email protected]', name: 'Mailtrap Test' },
4+
client = Mailtrap::Client.new(api_key: 'your-api-key')
5+
6+
# Set your API credentials as environment variables
7+
# export MAILTRAP_API_KEY='your-api-key'
8+
#
9+
# client = Mailtrap::Client.new
10+
# Bulk sending (@see https://help.mailtrap.io/article/113-sending-streams)
11+
# client = Mailtrap::Client.new(bulk: true)
12+
# Sandbox sending (@see https://help.mailtrap.io/article/109-getting-started-with-mailtrap-email-testing)
13+
# client = Mailtrap::Client.new(sandbox: true, inbox_id: 12)
14+
15+
# Batch sending with Mailtrap::Mail::Batch::Base
16+
mail = Mailtrap::Mail::Batch::Base.new(
17+
from: { email: '[email protected]', name: 'Mailtrap Test' },
718
subject: 'You are awesome!',
819
text: 'Congrats for sending test email with Mailtrap!',
920
category: 'Integration Test',
@@ -21,26 +32,75 @@
2132
}
2233
)
2334

24-
client = Mailtrap::Client.new(api_key: 'your-api-key')
35+
client.send_batch(mail, [
36+
Mailtrap::Mail::Base.new(
37+
to: [
38+
{ email: '[email protected]', name: 'recipient1' }
39+
]
40+
),
41+
Mailtrap::Mail::Base.new(
42+
to: [
43+
{ email: '[email protected]', name: 'recipient2' }
44+
]
45+
)
46+
])
2547

26-
# Custom host / port
27-
# client = Mailtrap::Client.new(api_key: 'your-api-key', api_host: 'alternative.host.mailtrap.io', api_port: 8080)
48+
# Batch sending with Mailtrap::Mail::Batch::FromTemplate
49+
mail = Mailtrap::Mail::Batch::FromTemplate.new(
50+
from: { email: '[email protected]', name: 'Mailtrap Test' },
51+
reply_to: { email: '[email protected]', name: 'Mailtrap Reply-To' },
52+
template_uuid: '339c8ab0-e73c-4269-984e-0d2446aacf2c',
53+
template_variables: {
54+
'user_name' => 'John Doe'
55+
}
56+
)
2857

29-
# Bulk sending (@see https://help.mailtrap.io/article/113-sending-streams)
30-
# client = Mailtrap::Client.new(api_key: 'your-api-key', bulk: true)
58+
client.send_batch(mail, [
59+
Mailtrap::Mail::FromTemplate.new(
60+
to: [
61+
{ email: '[email protected]', name: 'recipient1' }
62+
]
63+
),
64+
Mailtrap::Mail::FromTemplate.new(
65+
to: [
66+
{ email: '[email protected]', name: 'recipient2' }
67+
],
68+
template_variables: {
69+
'user_name' => 'John Doe 1',
70+
'user_name2' => 'John Doe 2'
71+
}
72+
)
73+
])
3174

32-
# Sandbox sending (@see https://help.mailtrap.io/article/109-getting-started-with-mailtrap-email-testing)
33-
# client = Mailtrap::Client.new(api_key: 'your-api-key', sandbox: true, inbox_id: 12)
75+
# Convert Mailtrap::Mail::Base to batch and send
76+
mail = Mailtrap::Mail::Base.new(
77+
from: { email: '[email protected]', name: 'Mailtrap Test' },
78+
subject: 'You are awesome!',
79+
text: 'Congrats for sending test email with Mailtrap!',
80+
category: 'Integration Test',
81+
attachments: [
82+
{
83+
content: Base64.encode64('Attachment content'), # base64 encoded content or IO string
84+
filename: 'attachment.txt'
85+
}
86+
],
87+
headers: {
88+
'X-MT-Header': 'Custom header'
89+
},
90+
custom_variables: {
91+
year: 2022
92+
}
93+
)
3494

35-
client.send_batch(mail, [
95+
client.send_batch(mail.to_batch, [
3696
Mailtrap::Mail::Base.new(
3797
to: [
38-
{ email: 'your@email.com', name: 'Your name' }
98+
{ email: 'xegoxo7905@coasah.com', name: 'recipient1' }
3999
]
40100
),
41101
Mailtrap::Mail::Base.new(
42102
to: [
43-
{ email: 'your2@email.com', name: 'Your name' }
103+
{ email: 'xegoxo7905@coasah.com', name: 'recipient2' }
44104
]
45105
)
46106
])

lib/mailtrap.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
require_relative 'mailtrap/action_mailer' if defined? ActionMailer
44
require_relative 'mailtrap/mail'
5+
require_relative 'mailtrap/mail/batch/base'
6+
require_relative 'mailtrap/mail/batch/from_template'
57
require_relative 'mailtrap/errors'
68
require_relative 'mailtrap/version'
79
require_relative 'mailtrap/email_templates_api'

lib/mailtrap/client.rb

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,26 @@ def initialize( # rubocop:disable Metrics/ParameterLists
5151
@http_clients = {}
5252
end
5353

54-
# Sends a batch of emails
55-
# @param base [Mailtrap::Mail::Base] The base email configuration
56-
# @param requests [Array<Mailtrap::Mail::Base>] Array of individual email requests
57-
# @return [Hash] The JSON response
54+
# Sends a batch of emails.
55+
#
56+
# @param base [Mailtrap::Mail::Batch::Base, Mailtrap::Mail::Batch::FromTemplate] The base email configuration for the batch. Must be a Mailtrap::Mail::Batch::Base or Mailtrap::Mail::Batch::FromTemplate object. # rubocop:disable Layout/LineLength
57+
# @param requests [Array<Mailtrap::Mail::Batch::Base>, Array<Mailtrap::Mail::Batch::FromTemplate>] Array of individual email requests. All elements must be of the same type as base. # rubocop:disable Layout/LineLength
58+
# @return [Hash] The JSON response from the API.
5859
# @!macro api_errors
59-
# @raise [Mailtrap::MailSizeError] If the message is too large
60-
# @raise [ArgumentError] If the mail is not a Mail::Base object
60+
# @raise [Mailtrap::MailSizeError] If the message is too large.
61+
# @raise [ArgumentError] If base or requests are not the correct type.
6162
def send_batch(base, requests)
62-
raise ArgumentError, 'base should be Mailtrap::Mail::Base object' unless base.is_a?(Mail::Base)
63+
unless base.is_a?(Mail::Batch::Base)
64+
raise ArgumentError,
65+
'base should be Mailtrap::Mail::Batch::Base or Mailtrap::Mail::FromTemplate object'
66+
end
6367

6468
unless requests.all?(Mail::Base)
6569
raise ArgumentError,
66-
'requests should be an array of Mailtrap::Mail::Base objects'
70+
'requests should be an array of Mailtrap::Mail::Batch::Base or Mailtrap::Mail::FromTemplate objects'
6771
end
68-
6972
perform_request(:post, api_host, batch_request_path, {
70-
base: compact_with_empty_arrays(base.as_json),
73+
base: base.as_json.except('to', 'cc', 'bcc'),
7174
requests:
7275
})
7376
end
@@ -209,10 +212,6 @@ def json_response(body)
209212
JSON.parse(body, symbolize_names: true)
210213
end
211214

212-
def compact_with_empty_arrays(hash)
213-
hash.reject { |_, v| v.nil? || (v.is_a?(Array) && v.empty?) }
214-
end
215-
216215
def validate_args!(api_key, api_port, bulk, sandbox, inbox_id)
217216
raise ArgumentError, 'api_key is required' if api_key.nil?
218217
raise ArgumentError, 'api_port is required' if api_port.nil?

lib/mailtrap/mail/base.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,23 @@ def add_attachment(content:, filename:, type: nil, disposition: nil, content_id:
7777

7878
attachment
7979
end
80+
81+
# Converts this Mail object to a Mailtrap::Mail::Batch::Base object for batch sending.
82+
#
83+
# @return [Mailtrap::Mail::Batch::Base] A new batch email object with the same properties as this mail.
84+
def to_batch
85+
Mailtrap::Mail::Batch::Base.new(
86+
from:,
87+
reply_to:,
88+
subject:,
89+
text:,
90+
html:,
91+
attachments:,
92+
headers:,
93+
custom_variables:,
94+
category:
95+
)
96+
end
8097
end
8198
end
8299
end

lib/mailtrap/mail/batch/base.rb

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# frozen_string_literal: true
2+
3+
require 'json'
4+
5+
module Mailtrap
6+
module Mail
7+
module Batch
8+
# General properties of all emails in the batch. Each of them can be overridden in requests for individual emails.
9+
#
10+
# @!macro [new] batch_email_base_properties
11+
# @!attribute [rw] from
12+
# @return [String, nil] The sender email address.
13+
# @!attribute [rw] reply_to
14+
# @return [String, nil] The reply-to email address.
15+
# @!attribute [rw] headers
16+
# @return [Hash] The custom headers for the email.
17+
# @!attribute [rw] custom_variables
18+
# @return [Hash] The custom variables for the email.
19+
# @!macro [new] batch_email_base_initialize_params
20+
# @param from [String, nil] The sender email address.
21+
# @param reply_to [String, nil] The reply-to email address.
22+
# @param attachments [Array<Hash>] The attachments for the email.
23+
# @param headers [Hash] The custom headers for the email.
24+
# @param custom_variables [Hash] The custom variables for the email.
25+
class Base < Mailtrap::Mail::Base
26+
# @!macro batch_email_base_properties
27+
# @!attribute [rw] text
28+
# @return [String, nil] The plain text body of the email.
29+
# @!attribute [rw] html
30+
# @return [String, nil] The HTML body of the email.
31+
# @!attribute [rw] category
32+
# @return [String, nil] The category of the email.
33+
# @!attribute [r] attachments
34+
# @return [Array<Mailtrap::Attachment>] The attachments for the email.
35+
# @!attribute [rw] subject
36+
# @return [String, nil] The subject of the email.
37+
attr_accessor :from, :reply_to, :headers, :custom_variables, :subject, :text, :html, :category
38+
attr_reader :attachments
39+
40+
# Initializes a new Mailtrap::Mail::Batch::Base object.
41+
#
42+
# @macro batch_email_base_initialize_params
43+
# @param subject [String, nil] The subject of the email.
44+
# @param text [String, nil] The plain text body of the email.
45+
# @param html [String, nil] The HTML body of the email.
46+
# @param category [String, nil] The category of the email.
47+
def initialize( # rubocop:disable Metrics/ParameterLists,Lint/MissingSuper
48+
from: nil,
49+
reply_to: nil,
50+
subject: nil,
51+
text: nil,
52+
html: nil,
53+
attachments: [],
54+
headers: {},
55+
custom_variables: {},
56+
category: nil
57+
)
58+
@from = from
59+
@reply_to = reply_to
60+
@subject = subject
61+
@text = text
62+
@html = html
63+
self.attachments = attachments
64+
@headers = headers
65+
@custom_variables = custom_variables
66+
@category = category
67+
end
68+
69+
# Returns a hash representation of the batch email suitable for JSON serialization'.
70+
# @return [Hash] The batch email as a hash.
71+
def as_json
72+
super.except('to', 'cc', 'bcc')
73+
end
74+
end
75+
end
76+
end
77+
end
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# frozen_string_literal: true
2+
3+
module Mailtrap
4+
module Mail
5+
module Batch
6+
# Batch email class for sending emails using templates.
7+
# Inherits all properties from Batch::Base and adds template-specific fields.
8+
# @!macro batch_email_base_properties
9+
# @!attribute [rw] template_uuid
10+
# @return [String, nil] UUID of email template.
11+
# @!attribute [rw] template_variables
12+
# @return [Hash] Optional template variables that will be used to generate actual subject, text and html from email template. # rubocop:disable Layout/LineLength
13+
class FromTemplate < Base
14+
attr_accessor :template_uuid, :template_variables
15+
16+
# Initializes a new Mailtrap::Mail::Batch::FromTemplate object.
17+
#
18+
# @macro batch_email_base_initialize_params
19+
# @param template_uuid [String, nil] UUID of email template.
20+
# @param template_variables [Hash] Optional template variables for generating email content.
21+
def initialize( # rubocop:disable Metrics/ParameterLists
22+
from: nil,
23+
reply_to: nil,
24+
attachments: [],
25+
headers: {},
26+
custom_variables: {},
27+
template_uuid: nil,
28+
template_variables: {}
29+
)
30+
super(
31+
from:,
32+
reply_to:,
33+
attachments:,
34+
headers:,
35+
custom_variables:
36+
)
37+
@template_uuid = template_uuid
38+
@template_variables = template_variables
39+
end
40+
41+
# Returns a hash representation of the template-based batch email suitable for JSON serialization.
42+
# @return [Hash] The template-based batch email as a hash.
43+
def as_json
44+
super.merge(
45+
{
46+
'template_uuid' => template_uuid,
47+
'template_variables' => template_variables
48+
}
49+
).compact
50+
end
51+
end
52+
end
53+
end
54+
end

lib/mailtrap/mail/from_template.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ def as_json
3939
}
4040
).compact
4141
end
42+
43+
# Converts this template-based Mail object to a Mailtrap::Mail::Batch::FromTemplate object for batch sending.
44+
#
45+
# @return [Mailtrap::Mail::Batch::FromTemplate] A new batch email object with the same properties as this template mail. # rubocop:disable Layout/LineLength
46+
def to_batch
47+
Mailtrap::Mail::Batch::FromTemplate.new(
48+
from:,
49+
reply_to:,
50+
attachments:,
51+
headers:,
52+
custom_variables:,
53+
template_uuid:,
54+
template_variables:
55+
)
56+
end
4257
end
4358
end
4459
end

spec/fixtures/vcr_cassettes/Mailtrap_Client/_send_batch/when_in_bulk_mode/successfully_sends_a_batch_of_emails.yml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)