Skip to content

Commit 691ddc0

Browse files
Ihor BilousIhor Bilous
authored andcommitted
Fix issue #52: Update README.md with new structure
1 parent 2dd7b89 commit 691ddc0

1 file changed

Lines changed: 137 additions & 14 deletions

File tree

README.md

Lines changed: 137 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![test](https://github.com/railsware/mailtrap-python/actions/workflows/main.yml/badge.svg)](https://github.com/railsware/mailtrap-python/actions/workflows/main.yml)
1+
[![test](https://github.com/mailtrap/mailtrap-python/actions/workflows/main.yml/badge.svg)](https://github.com/mailtrap/mailtrap-python/actions/workflows/main.yml)
22
[![PyPI](https://shields.io/pypi/v/mailtrap)](https://pypi.org/project/mailtrap/)
33
[![downloads](https://shields.io/pypi/dm/mailtrap)](https://pypi.org/project/mailtrap/)
44

@@ -27,34 +27,86 @@ pip install mailtrap
2727

2828
## Usage
2929

30-
### Minimal
30+
### Minimal usage (Transactional sending)
3131

3232
```python
3333
import mailtrap as mt
3434

35-
# create mail object
35+
API_TOKEN = "<YOUR_API_TOKEN>" # your API key here https://mailtrap.io/api-tokens
36+
37+
client = mt.MailtrapClient(token=API_TOKEN)
38+
39+
# Create mail object
3640
mail = mt.Mail(
37-
sender=mt.Address(email="mailtrap@example.com", name="Mailtrap Test"),
38-
to=[mt.Address(email="your@email.com")],
41+
sender=mt.Address(email="sender@example.com", name="John Smith"),
42+
to=[mt.Address(email="recipient@example.com")],
3943
subject="You are awesome!",
4044
text="Congrats for sending test email with Mailtrap!",
4145
)
4246

43-
# create client and send
44-
client = mt.MailtrapClient(token="your-api-key")
4547
client.send(mail)
4648
```
4749

48-
### Full
50+
### Sandbox vs Production (easy switching)
51+
52+
Mailtrap lets you test safely in the Email Sandbox and then switch to Production (Sending).
53+
Remove the inbox_id field or set it to None. Then, remove the sandbox field or set it to False.
54+
You can change the arguments in the code or via another way. Here is an example using environment variables.
55+
56+
Set next environment variables:
57+
```
58+
MAILTRAP_API_KEY=your_api_token # https://mailtrap.io/api-tokens
59+
MAILTRAP_USE_SANDBOX=true # true/false toggle
60+
MAILTRAP_INBOX_ID=123456 # Only needed for sandbox
61+
```
62+
63+
Bootstrap logic:
64+
```python
65+
import os
66+
import mailtrap as mt
67+
68+
API_KEY = os.environ["MAILTRAP_API_KEY"]
69+
IS_SANDBOX = os.environ.get("MAILTRAP_USE_SANDBOX", "true").lower() == "true"
70+
INBOX_ID = os.environ.get("MAILTRAP_INBOX_ID")
71+
72+
client = mt.MailtrapClient(
73+
token=API_KEY,
74+
sandbox=IS_SANDBOX,
75+
inbox_id=INBOX_ID, # None is ignored for production
76+
)
77+
78+
# Create mail object
79+
mail = mt.Mail(
80+
sender=mt.Address(email="sender@example.com", name="John Smith"),
81+
to=[mt.Address(email="recipient@example.com")],
82+
subject="You are awesome!",
83+
text="Congrats for sending test email with Mailtrap!",
84+
)
85+
86+
client.send(mail)
87+
```
88+
89+
Bulk stream example (optional) differs only by setting `bulk=True`:
90+
`bulk_client = mt.MailtrapClient(token=API_KEY, bulk=True)`
91+
92+
Recommendations:
93+
- Use separate API tokens for Production and Sandbox.
94+
- Keep initialisation in a single factory object/service so that switching is centralised.
95+
96+
### Full-featured usage example
4997

5098
```python
5199
import base64
100+
import os
52101
from pathlib import Path
53102

54103
import mailtrap as mt
55104

105+
client = mt.MailtrapClient(token=os.environ["MAILTRAP_API_KEY"])
106+
56107
welcome_image = Path(__file__).parent.joinpath("welcome.png").read_bytes()
57108

109+
58110
mail = mt.Mail(
59111
sender=mt.Address(email="mailtrap@example.com", name="Mailtrap Test"),
60112
to=[mt.Address(email="your@email.com", name="Your name")],
@@ -100,15 +152,17 @@ mail = mt.Mail(
100152
custom_variables={"year": 2023},
101153
)
102154

103-
client = mt.MailtrapClient(token="your-api-key")
104155
client.send(mail)
105156
```
106157

107-
### Using email template
158+
### Minimal usage of email template
108159

109160
```python
161+
import os
110162
import mailtrap as mt
111163

164+
client = mt.MailtrapClient(token=os.environ["MAILTRAP_API_KEY"])
165+
112166
# create mail object
113167
mail = mt.MailFromTemplate(
114168
sender=mt.Address(email="mailtrap@example.com", name="Mailtrap Test"),
@@ -117,21 +171,90 @@ mail = mt.MailFromTemplate(
117171
template_variables={"user_name": "John Doe"},
118172
)
119173

120-
# create client and send
121-
client = mt.MailtrapClient(token="your-api-key")
122174
client.send(mail)
123175
```
124176

177+
### All usage examples
178+
179+
Refer to the [examples](examples) folder for the source code of this and other advanced examples.
180+
181+
### Sending API
182+
- [Sending](examples/sending.py)
183+
184+
### Sandbox (Email Testing) API
185+
- [Attachments](examples/testing/attachments.py)
186+
- [Inboxes](examples/testing/inboxes.py)
187+
- [Messages](examples/testing/messages.py)
188+
- [Projects](examples/testing/projects.py)
189+
190+
### Contacts API
191+
- [Contacts](examples/contacts/contacts.py)
192+
- [Contact Events](examples/contacts/contact_events.py)
193+
- [Contact Exports](examples/contacts/contact_exports.py)
194+
- [Contact Fields](examples/contacts/contact_fields.py)
195+
- [Contact Imports](examples/contacts/contact_imports.py)
196+
- [Contact Lists](examples/contacts/contact_lists.py)
197+
198+
### Email Templates API
199+
- [Email Templates](examples/email_templates/templates.py)
200+
201+
### Suppressions API
202+
- [Suppressions](examples/suppressions/suppressions.py)
203+
204+
### General API
205+
- [Account Accesses](examples/general/account_accesses.py)
206+
- [Accounts](examples/general/accounts.py)
207+
- [Billing](examples/general/billing.py)
208+
- [Permissions](examples/general/permissions.py)
209+
210+
## Supported functionality
211+
212+
This Python package offers integration with the [official API](https://api-docs.mailtrap.io/) for [Mailtrap](https://mailtrap.io).
213+
214+
Quickly integrate Mailtrap with your Python app.
215+
216+
Currently, with this SDK you can:
217+
- Email API/SMTP
218+
- Send an email (Transactional and Bulk streams)
219+
- Send an email with a template (Transactional and Bulk streams)
220+
- Send a batch of emails (Transactional and Bulk streams)
221+
- Send a batch of emails with a template (Transactional and Bulk streams)
222+
- Email Sandbox (Testing)
223+
- Send an email
224+
- Send an email with a template
225+
- Send a batch of emails
226+
- Send a batch of emails with a template
227+
- Messages management
228+
- Inboxes management
229+
- Projects management
230+
- Attachments management
231+
- Contacts
232+
- Contacts management
233+
- Contact Lists management
234+
- Contact Fields management
235+
- Contact Events management
236+
- Contact Exports management
237+
- Contact Imports management
238+
- Suppressions
239+
- Suppressions management (find and delete)
240+
- Templates
241+
- Templates management
242+
- General
243+
- Account access management
244+
- Permissions management
245+
- List accounts you have access to
246+
- Get current billing information
247+
125248
## Contributing
126249

127-
Bug reports and pull requests are welcome on [GitHub](https://github.com/railsware/mailtrap-python). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](CODE_OF_CONDUCT.md).
250+
Bug reports and pull requests are welcome on [GitHub](https://github.com/mailtrap/mailtrap-python). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](CODE_OF_CONDUCT.md).
128251

129252
### Development Environment
130253

131254
#### Clone the repo
132255

133256
```bash
134-
https://github.com/railsware/mailtrap-python.git
257+
https://github.com/mailtrap/mailtrap-python.git
135258
cd mailtrap-python
136259
```
137260

0 commit comments

Comments
 (0)