Skip to content

Commit 12b27e9

Browse files
authored
Merge pull request #3 from i7an/batch-sending-api
Update Batch Sending API Examples
2 parents fe7b7b8 + c0e7556 commit 12b27e9

File tree

2 files changed

+85
-71
lines changed

2 files changed

+85
-71
lines changed

examples/batch.rb

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,67 +12,71 @@
1212
# Sandbox sending (@see https://help.mailtrap.io/article/109-getting-started-with-mailtrap-email-testing)
1313
# client = Mailtrap::Client.new(sandbox: true, inbox_id: 12)
1414

15-
# Batch sending with Mailtrap::Mail::Base
16-
mail = Mailtrap::Mail.batch_base_from_content(
15+
# Batch sending with text and html content
16+
batch_base = Mailtrap::Mail.batch_base_from_content(
1717
from: { email: '[email protected]', name: 'Mailtrap Test' },
1818
subject: 'You are awesome!',
1919
text: 'Congrats for sending test email with Mailtrap!',
20-
category: 'Integration Test',
21-
# attachments: [
22-
# {
23-
# content: Base64.encode64('Attachment content'), # base64 encoded content or IO string
24-
# filename: 'attachment.txt'
25-
# }
26-
# ],
27-
headers: {
28-
'X-MT-Header': 'Custom header'
29-
},
30-
custom_variables: {
31-
year: 2022
32-
}
20+
html: '<p>Congrats for sending test email with Mailtrap!</p>'
3321
)
3422

23+
File.open('attachment.txt') do |f|
24+
batch_base.add_attachment(content: f, filename: 'attachment.txt')
25+
end
26+
3527
client.send_batch(
36-
mail, [
28+
batch_base, [
3729
Mailtrap::Mail.from_content(
3830
to: [
39-
{ email: 'your@email.com', name: 'recipient1' }
31+
{ email: 'john.doe@email.com', name: 'John Doe' }
4032
]
4133
),
4234
Mailtrap::Mail::Base.new(
4335
to: [
44-
{ email: 'your@email.com', name: 'recipient2' }
36+
{ email: 'jane.doe@email.com', name: 'Jane Doe' }
4537
]
46-
)
38+
),
39+
{
40+
to: [
41+
{ email: '[email protected]', name: 'David Doe' }
42+
]
43+
}
4744
]
4845
)
4946

50-
# Batch sending with Mailtrap::Mail::Base
51-
mail = Mailtrap::Mail.batch_base_from_template(
47+
# Batch sending with template
48+
batch_base = Mailtrap::Mail.batch_base_from_template(
5249
from: { email: '[email protected]', name: 'Mailtrap Test' },
5350
reply_to: { email: '[email protected]', name: 'Mailtrap Reply-To' },
54-
template_uuid: '339c8ab0-e73c-4269-984e-0d2446aacf2c',
55-
template_variables: {
56-
'user_name' => 'John Doe'
57-
}
51+
template_uuid: '339c8ab0-e73c-4269-984e-0d2446aacf2c'
5852
)
5953

6054
client.send_batch(
61-
mail, [
62-
Mailtrap::Mail::Base.new(
55+
batch_base, [
56+
Mailtrap::Mail.from_template(
6357
to: [
64-
{ email: '[email protected]', name: 'recipient1' }
65-
]
58+
{ email: '[email protected]', name: 'John Doe' }
59+
],
60+
template_variables: {
61+
user_name: 'John Doe'
62+
}
6663
),
6764
Mailtrap::Mail::Base.new(
6865
to: [
69-
{ email: 'your@email.com', name: 'recipient2' }
66+
{ email: 'jane.doe@email.com', name: 'Jane Doe' }
7067
],
7168
template_variables: {
72-
'user_name' => 'John Doe 1',
73-
'user_name2' => 'John Doe 2'
69+
user_name: 'Jane Doe'
7470
}
75-
)
71+
),
72+
{
73+
to: [
74+
{ email: '[email protected]', name: 'David Doe' }
75+
],
76+
template_variables: {
77+
user_name: 'David Doe'
78+
}
79+
}
7680
]
7781
)
7882

@@ -81,24 +85,30 @@
8185
{
8286
from: { email: '[email protected]', name: 'Mailtrap Test' },
8387
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'
88+
template_uuid: '339c8ab0-e73c-4269-984e-0d2446aacf2c'
89+
}, [
90+
{
91+
to: [
92+
{ email: '[email protected]', name: 'John Doe' }
93+
],
94+
template_variables: {
95+
user_name: 'John Doe'
96+
}
8797
},
88-
},
89-
[
9098
{
9199
to: [
92-
{ email: '[email protected]', name: 'recipient1' }
93-
]
100+
{ email: '[email protected]', name: 'Jane Doe' }
101+
],
102+
template_variables: {
103+
user_name: 'Jane Doe'
104+
}
94105
},
95106
{
96107
to: [
97-
{ email: 'your@email.com', name: 'recipient2' }
108+
{ email: 'david.doe@email.com', name: 'David Doe' }
98109
],
99110
template_variables: {
100-
'user_name' => 'John Doe 1',
101-
'user_name2' => 'John Doe 2'
111+
user_name: 'David Doe'
102112
}
103113
}
104114
]

lib/mailtrap/client.rb

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -52,59 +52,63 @@ def initialize( # rubocop:disable Metrics/ParameterLists
5252
end
5353

5454
# Sends a batch of emails.
55-
# @example Sending batch emails using helpers
56-
# mail = Mailtrap::Mail.batch_base_from_template(
55+
# @example Batch sending with template
56+
# batch_base = Mailtrap::Mail.batch_base_from_template(
5757
# from: { email: '[email protected]', name: 'Mailtrap Test' },
5858
# reply_to: { email: '[email protected]', name: 'Mailtrap Reply-To' },
59-
# template_uuid: '339c8ab0-e73c-4269-984e-0d2446aacf2c',
60-
# template_variables: {
61-
# 'user_name' => 'John Doe'
62-
# }
59+
# template_uuid: '339c8ab0-e73c-4269-984e-0d2446aacf2c'
6360
# )
6461
#
6562
# client.send_batch(
66-
# mail,
67-
# [
68-
# Mailtrap::Mail.from_content(
63+
# batch_base, [
64+
# Mailtrap::Mail.from_template(
6965
# to: [
70-
# { email: '[email protected]', name: 'recipient1' }
71-
# ]
66+
# { email: '[email protected]', name: 'John Doe' }
67+
# ],
68+
# template_variables: {
69+
# user_name: 'John Doe'
70+
# }
7271
# ),
73-
# Mailtrap::Mail.from_template(
72+
# Mailtrap::Mail::Base.new(
7473
# to: [
75-
# { email: 'your@email.com', name: 'recipient2' }
74+
# { email: 'jane.doe@email.com', name: 'Jane Doe' }
7675
# ],
7776
# template_variables: {
78-
# 'user_name' => 'John Doe 1',
79-
# 'user_name2' => 'John Doe 2'
77+
# user_name: 'Jane Doe'
8078
# }
81-
# )
79+
# ),
80+
# {
81+
# to: [
82+
# { email: '[email protected]', name: 'David Doe' }
83+
# ],
84+
# template_variables: {
85+
# user_name: 'David Doe'
86+
# }
87+
# }
8288
# ]
8389
# )
8490
#
85-
# @example Sending batch emails using plain hashes
91+
# @example Passing the request parameters directly
8692
# client.send_batch(
8793
# {
8894
# from: { email: '[email protected]', name: 'Mailtrap Test' },
8995
# reply_to: { email: '[email protected]', name: 'Mailtrap Reply-To' },
90-
# template_uuid: '339c8ab0-e73c-4269-984e-0d2446aacf2c',
91-
# template_variables: {
92-
# 'user_name' => 'John Doe'
93-
# }
94-
# },
95-
# [
96+
# template_uuid: '339c8ab0-e73c-4269-984e-0d2446aacf2c'
97+
# }, [
9698
# {
9799
# to: [
98-
# { email: '[email protected]', name: 'recipient1' }
99-
# ]
100+
# { email: '[email protected]', name: 'John Doe' }
101+
# ],
102+
# template_variables: {
103+
# user_name: 'John Doe'
104+
# }
100105
# },
101106
# {
102107
# to: [
103-
# { email: 'your@email.com', name: 'recipient2' }
108+
# { email: 'jane.doe@email.com', name: 'Jane Doe' }
104109
# ],
105110
# template_variables: {
106-
# 'user_name' => 'John Doe 1',
107-
# 'user_name2' => 'John Doe 2'
111+
# user_name: 'Jane Doe'
108112
# }
109113
# }
110114
# ]

0 commit comments

Comments
 (0)