-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathminimal_sending.py
More file actions
47 lines (34 loc) · 1.31 KB
/
minimal_sending.py
File metadata and controls
47 lines (34 loc) · 1.31 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
import mailtrap as mt
from mailtrap.models.mail.mail import SendingMailResponse
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_}")
mail = mt.Mail(
sender=mt.Address(email="<SENDER_EMAIL>", name="<SENDER_NAME>"),
to=[mt.Address(email="<RECEIVER_EMAIL>")],
subject="You are awesome!",
text="Congrats for sending test email with Mailtrap!",
)
def send(client: mt.MailtrapClient, mail: mt.BaseMail) -> mt.SEND_ENDPOINT_RESPONSE:
return client.send(mail)
def send_via_sending_api(
client: mt.MailtrapClient, mail: mt.BaseMail
) -> SendingMailResponse:
"""Another way to send email via Sending API"""
return client.sending_api.send(mail)
if __name__ == "__main__":
client = get_client(SendingType.DEFAULT)
print(send(client, mail))
print(send_via_sending_api(client, mail))