-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbatch_minimal_sending.py
More file actions
60 lines (47 loc) · 1.67 KB
/
batch_minimal_sending.py
File metadata and controls
60 lines (47 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import mailtrap as mt
from mailtrap.models.mail.batch_mail import BatchSendResponse
API_TOKEN = "<YOUR_API_TOKEN>"
class SendingType:
DEFAULT = "default"
BULK = "bulk"
SANDBOX = "sandbox"
def get_client(type_: SendingType) -> mt.MailtrapClient:
if type_ == SendingType.DEFAULT:
return mt.MailtrapClient(token=API_TOKEN)
elif type_ == SendingType.BULK:
return mt.MailtrapClient(token=API_TOKEN, bulk=True)
elif type_ == SendingType.SANDBOX:
return mt.MailtrapClient(
token=API_TOKEN, sandbox=True, inbox_id="<YOUR_INBOX_ID>"
)
raise ValueError(f"Invalid sending type: {type_}")
batch_mail = mt.BatchSendEmailParams(
base=mt.BatchMail(
sender=mt.Address(email="<SENDER_EMAIL>", name="<SENDER_NAME>"),
subject="You are awesome!",
text="Congrats for sending test email with Mailtrap!",
category="Integration Test",
),
requests=[
mt.BatchEmailRequest(
to=[mt.Address(email="<RECEIVER_EMAIL_1>")],
),
mt.BatchEmailRequest(
to=[mt.Address(email="<RECEIVER_EMAIL_2>")],
),
],
)
def batch_send(
client: mt.MailtrapClient,
mail: mt.BatchSendEmailParams,
) -> mt.BATCH_SEND_ENDPOINT_RESPONSE:
return client.batch_send(mail)
def batch_send_via_sending_api(
client: mt.MailtrapClient, mail: mt.BatchSendEmailParams
) -> BatchSendResponse:
"""Another way to batch_send email via Sending API"""
return client.sending_api.batch_send(mail)
if __name__ == "__main__":
client = get_client(SendingType.DEFAULT)
print(batch_send(client, batch_mail))
print(batch_send_via_sending_api(client, batch_mail))