Skip to content

Commit 6e173c3

Browse files
Ihor BilousIhor Bilous
authored andcommitted
Fix issue #52: Add detailed examples for email sending
1 parent d6962b9 commit 6e173c3

File tree

9 files changed

+381
-84
lines changed

9 files changed

+381
-84
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,14 @@ client.send(mail)
179179
Refer to the [examples](examples) folder for the source code of this and other advanced examples.
180180

181181
### Sending API
182-
- [Sending](examples/sending.py)
182+
- [Sending minimal](examples/sending/minimal_sending.py)
183+
- [Sending advanced](examples/sending/advanced_sending.py)
184+
- [Sending using template](examples/sending/sending_with_template.py)
185+
186+
### Batch Sending API
187+
- [Batch sending minimal](examples/sending/batch_minimal_sending.py)
188+
- [Batch sending advanced](examples/sending/batch_advanced_sending.py)
189+
- [Bathc sending using template](examples/sending/batch_sending_with_template.py)
183190

184191
### Sandbox (Email Testing) API
185192
- [Attachments](examples/testing/attachments.py)

examples/sending.py

Lines changed: 0 additions & 83 deletions
This file was deleted.

examples/sending/__init__.py

Whitespace-only changes.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import base64
2+
from pathlib import Path
3+
4+
import mailtrap as mt
5+
6+
API_TOKEN = "<YOUR_API_TOKEN>"
7+
8+
9+
class SendingType:
10+
DEFAULT = "default"
11+
BULK = "bulk"
12+
SANDBOX = "sandbox"
13+
14+
15+
def get_client(type_: SendingType) -> mt.MailtrapClient:
16+
if type_ == SendingType.DEFAULT:
17+
return mt.MailtrapClient(token=API_TOKEN)
18+
elif type_ == SendingType.BULK:
19+
return mt.MailtrapClient(token=API_TOKEN, bulk=True)
20+
elif type_ == SendingType.SANDBOX:
21+
return mt.MailtrapClient(
22+
token=API_TOKEN,
23+
sandbox=True,
24+
inbox_id="<YOUR_INBOX_ID>",
25+
)
26+
27+
28+
# Image should be in the same level in directory like this python file.
29+
welcome_image = Path(__file__).parent.joinpath("welcome.png").read_bytes()
30+
31+
mail = mt.Mail(
32+
sender=mt.Address(email="<SENDER_EMAIL>", name="<SENDER_NAME>"),
33+
to=[mt.Address(email="<RECEIVER_EMAIL>")],
34+
cc=[mt.Address(email="[email protected]", name="Copy to")],
35+
bcc=[mt.Address(email="[email protected]", name="Hidden Recipient")],
36+
subject="You are awesome!",
37+
text="Congrats for sending test email with Mailtrap!",
38+
html="""
39+
<!doctype html>
40+
<html>
41+
<head>
42+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
43+
</head>
44+
<body style="font-family: sans-serif;">
45+
<div style="display: block; margin: auto; max-width: 600px;" class="main">
46+
<h1 style="font-size: 18px; font-weight: bold; margin-top: 20px">
47+
Congrats for sending test email with Mailtrap!
48+
</h1>
49+
<p>
50+
Inspect it using the tabs you see above and
51+
learn how this email can be improved.
52+
</p>
53+
<img alt="Inspect with Tabs" src="cid:welcome.png" style="width: 100%;">
54+
<p>
55+
Now send your email using our fake SMTP server and integration of your choice!
56+
</p>
57+
<p>Good luck! Hope it works.</p>
58+
</div>
59+
<!-- Example of invalid for email html/css, will be detected by Mailtrap: -->
60+
<style>
61+
.main { background-color: white; }
62+
a:hover { border-left-width: 1em; min-height: 2em; }
63+
</style>
64+
</body>
65+
</html>
66+
""",
67+
category="Test",
68+
attachments=[
69+
mt.Attachment(
70+
content=base64.b64encode(welcome_image),
71+
filename="welcome.png",
72+
disposition=mt.Disposition.INLINE,
73+
mimetype="image/png",
74+
content_id="welcome.png",
75+
)
76+
],
77+
headers={"X-MT-Header": "Custom header"},
78+
custom_variables={"year": 2023},
79+
)
80+
81+
82+
def send(
83+
client: mt.MailtrapClient,
84+
mail: mt.BaseMail,
85+
) -> mt.SEND_ENDPOINT_RESPONSE:
86+
return client.send(mail)
87+
88+
89+
if __name__ == "__main__":
90+
client = get_client(SendingType.DEFAULT)
91+
print(send(client, mail))
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import base64
2+
from pathlib import Path
3+
4+
import mailtrap as mt
5+
6+
API_TOKEN = "<YOUR_API_TOKEN>"
7+
8+
9+
class SendingType:
10+
DEFAULT = "default"
11+
BULK = "bulk"
12+
SANDBOX = "sandbox"
13+
14+
15+
def get_client(type_: SendingType) -> mt.MailtrapClient:
16+
if type_ == SendingType.DEFAULT:
17+
return mt.MailtrapClient(token=API_TOKEN)
18+
elif type_ == SendingType.BULK:
19+
return mt.MailtrapClient(token=API_TOKEN, bulk=True)
20+
elif type_ == SendingType.SANDBOX:
21+
return mt.MailtrapClient(
22+
token=API_TOKEN,
23+
sandbox=True,
24+
inbox_id="<YOUR_INBOX_ID>",
25+
)
26+
27+
28+
# Image should be in the same level in directory like this python file.
29+
welcome_image = Path(__file__).parent.joinpath("welcome.png").read_bytes()
30+
31+
batch_mail = mt.BatchSendEmailParams(
32+
base=mt.BatchMail(
33+
sender=mt.Address(email="<SENDER_EMAIL>", name="<SENDER_NAME>"),
34+
cc=[mt.Address(email="[email protected]", name="Copy to")],
35+
bcc=[mt.Address(email="[email protected]", name="Hidden Recipient")],
36+
subject="You are awesome!",
37+
text="Congrats for sending test email with Mailtrap!",
38+
html="""
39+
<!doctype html>
40+
<html>
41+
<head>
42+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
43+
</head>
44+
<body style="font-family: sans-serif;">
45+
<div style="display: block; margin: auto; max-width: 600px;" class="main">
46+
<h1 style="font-size: 18px; font-weight: bold; margin-top: 20px">
47+
Congrats for sending test email with Mailtrap!
48+
</h1>
49+
<p>
50+
Inspect it using the tabs you see above and
51+
learn how this email can be improved.
52+
</p>
53+
<img alt="Inspect with Tabs" src="cid:welcome.png" style="width: 100%;">
54+
<p>
55+
Now send your email using our fake SMTP server
56+
and integration of your choice!
57+
</p>
58+
<p>Good luck! Hope it works.</p>
59+
</div>
60+
<!-- Example of invalid for email html/css, will be detected by Mailtrap: -->
61+
<style>
62+
.main { background-color: white; }
63+
a:hover { border-left-width: 1em; min-height: 2em; }
64+
</style>
65+
</body>
66+
</html>
67+
""",
68+
category="Test",
69+
attachments=[
70+
mt.Attachment(
71+
content=base64.b64encode(welcome_image),
72+
filename="welcome.png",
73+
disposition=mt.Disposition.INLINE,
74+
mimetype="image/png",
75+
content_id="welcome.png",
76+
)
77+
],
78+
headers={"X-MT-Header": "Custom header"},
79+
custom_variables={"year": 2023},
80+
),
81+
requests=[
82+
mt.BatchEmailRequest(
83+
to=[mt.Address(email="<RECEIVER_EMAIL>")],
84+
),
85+
],
86+
)
87+
88+
89+
def batch_send(
90+
client: mt.MailtrapClient,
91+
mail: mt.BatchSendEmailParams,
92+
) -> mt.BATCH_SEND_ENDPOINT_RESPONSE:
93+
return client.batch_send(mail)
94+
95+
96+
if __name__ == "__main__":
97+
client = get_client(SendingType.DEFAULT)
98+
print(batch_send(client, batch_mail))
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import mailtrap as mt
2+
3+
API_TOKEN = "<YOUR_API_TOKEN>"
4+
5+
6+
class SendingType:
7+
DEFAULT = "default"
8+
BULK = "bulk"
9+
SANDBOX = "sandbox"
10+
11+
12+
def get_client(type_: SendingType) -> mt.MailtrapClient:
13+
if type_ == SendingType.DEFAULT:
14+
return mt.MailtrapClient(token=API_TOKEN)
15+
elif type_ == SendingType.BULK:
16+
return mt.MailtrapClient(token=API_TOKEN, bulk=True)
17+
elif type_ == SendingType.SANDBOX:
18+
return mt.MailtrapClient(
19+
token=API_TOKEN, sandbox=True, inbox_id="<YOUR_INBOX_ID>"
20+
)
21+
22+
23+
batch_mail = mt.BatchSendEmailParams(
24+
base=mt.BatchMail(
25+
sender=mt.Address(email="<SENDER_EMAIL>", name="<SENDER_NAME>"),
26+
subject="You are awesome!",
27+
text="Congrats for sending test email with Mailtrap!",
28+
category="Integration Test",
29+
),
30+
requests=[
31+
mt.BatchEmailRequest(
32+
to=[mt.Address(email="<RECEIVER_EMAIL>")],
33+
),
34+
],
35+
)
36+
37+
38+
def batch_send(
39+
client: mt.MailtrapClient,
40+
mail: mt.BatchSendEmailParams,
41+
) -> mt.BATCH_SEND_ENDPOINT_RESPONSE:
42+
return client.batch_send(mail)
43+
44+
45+
if __name__ == "__main__":
46+
client = get_client(SendingType.DEFAULT)
47+
print(batch_send(client, batch_mail))
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import mailtrap as mt
2+
3+
API_TOKEN = "<YOUR_API_TOKEN>"
4+
5+
6+
class SendingType:
7+
DEFAULT = "default"
8+
BULK = "bulk"
9+
SANDBOX = "sandbox"
10+
11+
12+
def get_client(type_: SendingType) -> mt.MailtrapClient:
13+
if type_ == SendingType.DEFAULT:
14+
return mt.MailtrapClient(token=API_TOKEN)
15+
elif type_ == SendingType.BULK:
16+
return mt.MailtrapClient(token=API_TOKEN, bulk=True)
17+
elif type_ == SendingType.SANDBOX:
18+
return mt.MailtrapClient(
19+
token=API_TOKEN, sandbox=True, inbox_id="<YOUR_INBOX_ID>"
20+
)
21+
22+
23+
batch_mail = mt.BatchSendEmailParams(
24+
base=mt.BatchMailFromTemplate(
25+
sender=mt.Address(email="<SENDER_EMAIL>", name="<SENDER_NAME>"),
26+
template_uuid="<YOUR_TEMPLATE_UUID>",
27+
template_variables={
28+
"company_info_name": "Test_Company_info_name",
29+
"name": "Test_Name",
30+
"company_info_address": "Test_Company_info_address",
31+
"company_info_city": "Test_Company_info_city",
32+
"company_info_zip_code": "Test_Company_info_zip_code",
33+
"company_info_country": "Test_Company_info_country",
34+
},
35+
),
36+
requests=[
37+
mt.BatchEmailRequest(
38+
to=[mt.Address(email="<RECEIVER_EMAIL>")],
39+
subject="You are awesome!",
40+
text="Congrats for sending test email with Mailtrap!",
41+
category="Integration Test",
42+
),
43+
],
44+
)
45+
46+
47+
def batch_send(
48+
client: mt.MailtrapClient,
49+
mail: mt.BatchSendEmailParams,
50+
) -> mt.BATCH_SEND_ENDPOINT_RESPONSE:
51+
return client.batch_send(mail)
52+
53+
54+
if __name__ == "__main__":
55+
client = get_client(SendingType.DEFAULT)
56+
print(batch_send(client, batch_mail))

0 commit comments

Comments
 (0)