Skip to content

Commit 7180789

Browse files
Ihor BilousIhor Bilous
authored andcommitted
Fix issue #52: Add new way of sending mails to examples and README.md
1 parent 1fb507a commit 7180789

File tree

7 files changed

+70
-18
lines changed

7 files changed

+70
-18
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,46 @@ mail = mt.MailFromTemplate(
174174
client.send(mail)
175175
```
176176

177+
### Sending email directly via SendingApi
178+
179+
This approach is newer. It can be useful when you expect the response to be model-based rather than dictionary-based, as in MailtrapClient.send().
180+
181+
```python
182+
import os
183+
import mailtrap as mt
184+
185+
client = mt.MailtrapClient(token=os.environ["MAILTRAP_API_KEY"])
186+
sending_api = client.sending_api
187+
188+
# create mail object
189+
mail = mt.Mail(
190+
sender=mt.Address(email="[email protected]", name="John Smith"),
191+
to=[mt.Address(email="[email protected]")],
192+
subject="You are awesome!",
193+
text="Congrats for sending test email with Mailtrap!",
194+
)
195+
196+
sending_api.send(mail)
197+
```
198+
#### Mailtrap sending responses difference
199+
200+
#### 1. `client.send()`
201+
**Response:**
202+
```python
203+
{
204+
"success": True,
205+
"message_ids": ["5162954175"]
206+
}
207+
```
208+
209+
#### 2. `client.sending_api.send()`
210+
**Response:**
211+
```python
212+
SendingMailResponse(success=True, message_ids=["5162955057"])
213+
```
214+
215+
The same situation applies to both `client.batch_send()` and `client.sending_api.batch_send()`.
216+
177217
### All usage examples
178218

179219
Refer to the [examples](examples) folder for the source code of this and other advanced examples.

examples/sending/advanced_sending.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ def get_client(type_: SendingType) -> mt.MailtrapClient:
2323
sandbox=True,
2424
inbox_id="<YOUR_INBOX_ID>",
2525
)
26-
else:
27-
raise ValueError(f"Invalid sending type: {type_}")
2826

2927

3028
# Image should be in the same level in directory like this python file.

examples/sending/batch_advanced_sending.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ def get_client(type_: SendingType) -> mt.MailtrapClient:
2323
sandbox=True,
2424
inbox_id="<YOUR_INBOX_ID>",
2525
)
26-
else:
27-
raise ValueError(f"Invalid sending type: {type_}")
2826

2927

3028
# Image should be in the same level in directory like this python file.
@@ -82,7 +80,10 @@ def get_client(type_: SendingType) -> mt.MailtrapClient:
8280
),
8381
requests=[
8482
mt.BatchEmailRequest(
85-
to=[mt.Address(email="<RECEIVER_EMAIL>")],
83+
to=[mt.Address(email="<RECEIVER_EMAIL_1>")],
84+
),
85+
mt.BatchEmailRequest(
86+
to=[mt.Address(email="<RECEIVER_EMAIL_2>")],
8687
),
8788
],
8889
)

examples/sending/batch_minimal_sending.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import mailtrap as mt
2+
from mailtrap.models.mail.batch_mail import BatchSendResponse
23

34
API_TOKEN = "<YOUR_API_TOKEN>"
45

@@ -18,8 +19,6 @@ def get_client(type_: SendingType) -> mt.MailtrapClient:
1819
return mt.MailtrapClient(
1920
token=API_TOKEN, sandbox=True, inbox_id="<YOUR_INBOX_ID>"
2021
)
21-
else:
22-
raise ValueError(f"Invalid sending type: {type_}")
2322

2423

2524
batch_mail = mt.BatchSendEmailParams(
@@ -31,7 +30,10 @@ def get_client(type_: SendingType) -> mt.MailtrapClient:
3130
),
3231
requests=[
3332
mt.BatchEmailRequest(
34-
to=[mt.Address(email="<RECEIVER_EMAIL>")],
33+
to=[mt.Address(email="<RECEIVER_EMAIL_1>")],
34+
),
35+
mt.BatchEmailRequest(
36+
to=[mt.Address(email="<RECEIVER_EMAIL_2>")],
3537
),
3638
],
3739
)
@@ -44,6 +46,14 @@ def batch_send(
4446
return client.batch_send(mail)
4547

4648

49+
def batch_send_via_sending_api(
50+
client: mt.MailtrapClient, mail: mt.BaseMail
51+
) -> BatchSendResponse:
52+
"""Another way to batch_send email via Sending API"""
53+
return client.sending_api.batch_send(mail)
54+
55+
4756
if __name__ == "__main__":
4857
client = get_client(SendingType.DEFAULT)
4958
print(batch_send(client, batch_mail))
59+
print(batch_send_via_sending_api(client, batch_mail))

examples/sending/batch_sending_with_template.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ def get_client(type_: SendingType) -> mt.MailtrapClient:
1818
return mt.MailtrapClient(
1919
token=API_TOKEN, sandbox=True, inbox_id="<YOUR_INBOX_ID>"
2020
)
21-
else:
22-
raise ValueError(f"Invalid sending type: {type_}")
2321

2422

2523
batch_mail = mt.BatchSendEmailParams(
@@ -37,10 +35,10 @@ def get_client(type_: SendingType) -> mt.MailtrapClient:
3735
),
3836
requests=[
3937
mt.BatchEmailRequest(
40-
to=[mt.Address(email="<RECEIVER_EMAIL>")],
41-
subject="You are awesome!",
42-
text="Congrats for sending test email with Mailtrap!",
43-
category="Integration Test",
38+
to=[mt.Address(email="<RECEIVER_EMAIL_1>")],
39+
),
40+
mt.BatchEmailRequest(
41+
to=[mt.Address(email="<RECEIVER_EMAIL_2>")],
4442
),
4543
],
4644
)

examples/sending/minimal_sending.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import mailtrap as mt
2+
from mailtrap.models.mail.mail import SendingMailResponse
23

34
API_TOKEN = "<YOUR_API_TOKEN>"
45

@@ -18,8 +19,6 @@ def get_client(type_: SendingType) -> mt.MailtrapClient:
1819
return mt.MailtrapClient(
1920
token=API_TOKEN, sandbox=True, inbox_id="<YOUR_INBOX_ID>"
2021
)
21-
else:
22-
raise ValueError(f"Invalid sending type: {type_}")
2322

2423

2524
mail = mt.Mail(
@@ -34,6 +33,14 @@ def send(client: mt.MailtrapClient, mail: mt.BaseMail) -> mt.SEND_ENDPOINT_RESPO
3433
return client.send(mail)
3534

3635

36+
def send_via_sending_api(
37+
client: mt.MailtrapClient, mail: mt.BaseMail
38+
) -> SendingMailResponse:
39+
"""Another way to send email via Sending API"""
40+
return client.sending_api.send(mail)
41+
42+
3743
if __name__ == "__main__":
3844
client = get_client(SendingType.DEFAULT)
3945
print(send(client, mail))
46+
print(send_via_sending_api(client, mail))

examples/sending/sending_with_template.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ def get_client(type_: SendingType) -> mt.MailtrapClient:
1818
return mt.MailtrapClient(
1919
token=API_TOKEN, sandbox=True, inbox_id="<YOUR_INBOX_ID>"
2020
)
21-
else:
22-
raise ValueError(f"Invalid sending type: {type_}")
2321

2422

2523
mail = mt.MailFromTemplate(

0 commit comments

Comments
 (0)