Skip to content

Commit 6995844

Browse files
committed
added batch_from_template and batch_from_content methods, formatting and error messages improvements
1 parent d7ca30a commit 6995844

File tree

5 files changed

+97
-66
lines changed

5 files changed

+97
-66
lines changed

examples/batch.rb

Lines changed: 66 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313
# client = Mailtrap::Client.new(sandbox: true, inbox_id: 12)
1414

1515
# Batch sending with Mailtrap::Mail::Base
16-
mail = Mailtrap::Mail::Base.new(
16+
mail = Mailtrap::Mail.batch_from_content(
1717
from: { email: '[email protected]', name: 'Mailtrap Test' },
1818
subject: 'You are awesome!',
1919
text: 'Congrats for sending test email with Mailtrap!',
2020
category: 'Integration Test',
21-
attachments: [
22-
{
23-
content: Base64.encode64('Attachment content'), # base64 encoded content or IO string
24-
filename: 'attachment.txt'
25-
}
26-
],
21+
# attachments: [
22+
# {
23+
# content: Base64.encode64('Attachment content'), # base64 encoded content or IO string
24+
# filename: 'attachment.txt'
25+
# }
26+
# ],
2727
headers: {
2828
'X-MT-Header': 'Custom header'
2929
},
@@ -32,21 +32,23 @@
3232
}
3333
)
3434

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-
])
35+
client.send_batch(
36+
mail, [
37+
Mailtrap::Mail.from_content(
38+
to: [
39+
{ email: '[email protected]', name: 'recipient1' }
40+
]
41+
),
42+
Mailtrap::Mail::Base.new(
43+
to: [
44+
{ email: '[email protected]', name: 'recipient2' }
45+
]
46+
)
47+
]
48+
)
4749

4850
# Batch sending with Mailtrap::Mail::Base
49-
mail = Mailtrap::Mail::Base.new(
51+
mail = Mailtrap::Mail.batch_from_template(
5052
from: { email: '[email protected]', name: 'Mailtrap Test' },
5153
reply_to: { email: '[email protected]', name: 'Mailtrap Reply-To' },
5254
template_uuid: '339c8ab0-e73c-4269-984e-0d2446aacf2c',
@@ -55,45 +57,49 @@
5557
}
5658
)
5759

58-
client.send_batch(mail, [
59-
Mailtrap::Mail::Base.new(
60-
to: [
61-
{ email: '[email protected]', name: 'recipient1' }
62-
]
63-
),
64-
Mailtrap::Mail::Base.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-
])
60+
client.send_batch(
61+
mail, [
62+
Mailtrap::Mail::Base.new(
63+
to: [
64+
{ email: '[email protected]', name: 'recipient1' }
65+
]
66+
),
67+
Mailtrap::Mail::Base.new(
68+
to: [
69+
{ email: '[email protected]', name: 'recipient2' }
70+
],
71+
template_variables: {
72+
'user_name' => 'John Doe 1',
73+
'user_name2' => 'John Doe 2'
74+
}
75+
)
76+
]
77+
)
7478

7579
# You can also pass the request parameters directly
76-
client.send_batch({
77-
from: { email: '[email protected]', name: 'Mailtrap Test' },
78-
reply_to: { email: '[email protected]', name: 'Mailtrap Reply-To' },
79-
template_uuid: '339c8ab0-e73c-4269-984e-0d2446aacf2c',
80-
template_variables: {
81-
'user_name' => 'John Doe'
82-
},
83-
},
84-
[
85-
{
86-
to: [
87-
{ email: '[email protected]', name: 'recipient1' }
88-
]
89-
},
90-
{
91-
to: [
92-
{ email: '[email protected]', name: 'recipient2' }
93-
],
94-
template_variables: {
95-
'user_name' => 'John Doe 1',
96-
'user_name2' => 'John Doe 2'
97-
}
98-
}
99-
])
80+
client.send_batch(
81+
{
82+
from: { email: '[email protected]', name: 'Mailtrap Test' },
83+
reply_to: { email: '[email protected]', name: 'Mailtrap Reply-To' },
84+
template_uuid: '339c8ab0-e73c-4269-984e-0d2446aacf2c',
85+
template_variables: {
86+
'user_name' => 'John Doe'
87+
},
88+
},
89+
[
90+
{
91+
to: [
92+
{ email: '[email protected]', name: 'recipient1' }
93+
]
94+
},
95+
{
96+
to: [
97+
{ email: '[email protected]', name: 'recipient2' }
98+
],
99+
template_variables: {
100+
'user_name' => 'John Doe 1',
101+
'user_name2' => 'John Doe 2'
102+
}
103+
}
104+
]
105+
)

lib/mailtrap/client.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,6 @@ def http_client_for(host)
187187
end
188188

189189
def select_api_host(bulk:, sandbox:)
190-
raise ArgumentError, 'bulk mode is not applicable for sandbox API' if bulk && sandbox
191-
192190
if sandbox
193191
SANDBOX_API_HOST
194192
elsif bulk
@@ -274,7 +272,7 @@ def json_response(body)
274272
def validate_args!(api_key, api_port, bulk, sandbox, inbox_id)
275273
raise ArgumentError, 'api_key is required' if api_key.nil?
276274
raise ArgumentError, 'api_port is required' if api_port.nil?
277-
raise ArgumentError, 'bulk mode is not applicable for sandbox API' if bulk && sandbox
275+
raise ArgumentError, 'bulk stream is not applicable for sandbox API' if bulk && sandbox
278276
raise ArgumentError, 'inbox_id is required for sandbox API' if sandbox && inbox_id.nil?
279277
end
280278
end

lib/mailtrap/mail.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,33 @@ def from_content( # rubocop:disable Metrics/ParameterLists
117117
)
118118
end
119119

120+
# Builds a mail object that will be sent using a pre-defined email
121+
# template. The template content (subject, text, html, category) is
122+
# defined in the Mailtrap dashboard and referenced by the template_uuid.
123+
# Template variables can be passed to customize the template content.
124+
# @example
125+
# mail = Mailtrap::Mail.batch_from_template(
126+
# from: { email: '[email protected]', name: 'Mailtrap Test' },
127+
# template_uuid: '2f45b0aa-bbed-432f-95e4-e145e1965ba2',
128+
# template_variables: {
129+
# 'user_name' => 'John Doe'
130+
# }
131+
# )
132+
def batch_from_template(**args)
133+
from_template(**args.except(:to, :cc, :bcc))
134+
end
135+
136+
# Builds a mail object with content including subject, text, html, and category.
137+
# @example
138+
# mail = Mailtrap::Mail.batch_from_content(
139+
# from: { email: '[email protected]', name: 'Mailtrap Test' },
140+
# subject: 'You are awesome!',
141+
# text: 'Congrats for sending test email with Mailtrap!'
142+
# )
143+
def batch_from_content(**args)
144+
from_content(**args.except(:to, :cc, :bcc))
145+
end
146+
120147
# Builds a mail object from Mail::Message
121148
# @param message [Mail::Message]
122149
# @return [Mailtrap::Mail::Base]

spec/mailtrap/client_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
described_class.new(api_key:, bulk: true, sandbox: true)
111111
end
112112

113-
it { expect { send }.to raise_error(ArgumentError, 'bulk mode is not applicable for sandbox API') }
113+
it { expect { send }.to raise_error(ArgumentError, 'bulk stream is not applicable for sandbox API') }
114114
end
115115
end
116116

@@ -311,11 +311,11 @@ def stub_post(path, status, body)
311311
it 'raises an error' do
312312
expect do
313313
client.send_batch(base_mail, recipients)
314-
end.to raise_error(ArgumentError, 'bulk mode is not applicable for sandbox API')
314+
end.to raise_error(ArgumentError, 'bulk stream is not applicable for sandbox API')
315315
end
316316
end
317317

318-
context 'when in bulk mode' do
318+
context 'when in bulk stream' do
319319
let(:client) { described_class.new(api_key:, bulk: true) }
320320

321321
it 'successfully sends a batch of emails', :vcr do

0 commit comments

Comments
 (0)