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 
3333import  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
3640mail =  mt.Mail(
37-     sender = mt.Address(
email = " [email protected] " , 
name = " Mailtrap Test" ),
 38-     to = [mt.Address(
email = " [email protected] " )],
 41+     sender = mt.Address(
email = " [email protected] " , 
name = " John Smith" ),
 42+     to = [mt.Address(
email = " [email protected] " )],
 43+     subject = " You are awesome!"  ,
44+     text = " Congrats for sending test email with Mailtrap!"  ,
45+ )
46+ 
47+ client.send(mail)
48+ ``` 
49+ 
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+ ``` bash 
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 = " [email protected] " , 
name = " John Smith" ),
 81+     to = [mt.Address(
email = " [email protected] " )],
 3982    subject = " You are awesome!"  ,
4083    text = " Congrats for sending test email with Mailtrap!"  ,
4184)
4285
43- #  create client and send
44- client =  mt.MailtrapClient(token = " your-api-key"  )
4586client.send(mail)
4687``` 
4788
48- ### Full  
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 
5199import  base64
100+ import  os
52101from  pathlib import  Path
53102
54103import  mailtrap as  mt
55104
105+ client =  mt.MailtrapClient(token = os.environ[" MAILTRAP_API_KEY"  ])
106+ 
56107welcome_image =  Path(__file__ ).parent.joinpath(" welcome.png"  ).read_bytes()
57108
109+ 
58110mail =  mt.Mail(
59111    sender = mt.Address(
email = " [email protected] " , 
name = " Mailtrap Test" ),
 60112    to = [mt.Address(
email = " [email protected] " , 
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"  )
104155client.send(mail)
105156``` 
106157
107- ### Using  email template 
158+ ### Minimal usage of  email template 
108159
109160``` python 
161+ import  os
110162import  mailtrap as  mt
111163
164+ client =  mt.MailtrapClient(token = os.environ[" MAILTRAP_API_KEY"  ])
165+ 
112166#  create mail object
113167mail =  mt.MailFromTemplate(
114168    sender = mt.Address(
email = " [email protected] " , 
name = " Mailtrap Test" ),
 @@ -117,21 +171,137 @@ 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"  )
122174client.send(mail)
123175``` 
124176
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+ 
217+ ### All usage examples  
218+ 
219+ Refer to the [ examples] ( examples )  folder for the source code of this and other advanced examples.
220+ 
221+ ### Sending API  
222+ -  [ Sending minimal] ( examples/sending/minimal_sending.py ) 
223+ -  [ Sending advanced] ( examples/sending/advanced_sending.py ) 
224+ -  [ Sending using template] ( examples/sending/sending_with_template.py ) 
225+ 
226+ ### Batch Sending API  
227+ -  [ Batch sending minimal] ( examples/sending/batch_minimal_sending.py ) 
228+ -  [ Batch sending advanced] ( examples/sending/batch_advanced_sending.py ) 
229+ -  [ Batch sending using template] ( examples/sending/batch_sending_with_template.py ) 
230+ 
231+ ### Sandbox (Email Testing) API  
232+ -  [ Attachments] ( examples/testing/attachments.py ) 
233+ -  [ Inboxes] ( examples/testing/inboxes.py ) 
234+ -  [ Messages] ( examples/testing/messages.py ) 
235+ -  [ Projects] ( examples/testing/projects.py ) 
236+ 
237+ ### Contacts API  
238+ -  [ Contacts] ( examples/contacts/contacts.py ) 
239+ -  [ Contact Events] ( examples/contacts/contact_events.py ) 
240+ -  [ Contact Exports] ( examples/contacts/contact_exports.py ) 
241+ -  [ Contact Fields] ( examples/contacts/contact_fields.py ) 
242+ -  [ Contact Imports] ( examples/contacts/contact_imports.py ) 
243+ -  [ Contact Lists] ( examples/contacts/contact_lists.py ) 
244+ 
245+ ### Email Templates API  
246+ -  [ Email Templates] ( examples/email_templates/templates.py ) 
247+ 
248+ ### Suppressions API  
249+ -  [ Suppressions] ( examples/suppressions/suppressions.py ) 
250+ 
251+ ### General API  
252+ -  [ Account Accesses] ( examples/general/account_accesses.py ) 
253+ -  [ Accounts] ( examples/general/accounts.py ) 
254+ -  [ Billing] ( examples/general/billing.py ) 
255+ -  [ Permissions] ( examples/general/permissions.py ) 
256+ 
257+ ## Supported functionality  
258+ 
259+ This Python package offers integration with the [ official API] ( https://api-docs.mailtrap.io/ )  for [ Mailtrap] ( https://mailtrap.io ) .
260+ 
261+ Quickly integrate Mailtrap with your Python app.
262+ 
263+ Currently, with this SDK you can:
264+ -  Email API/SMTP
265+   -  Send an email (Transactional and Bulk streams)
266+   -  Send an email with a template (Transactional and Bulk streams)
267+   -  Send a batch of emails (Transactional and Bulk streams)
268+   -  Send a batch of emails with a template (Transactional and Bulk streams)
269+ -  Email Sandbox (Testing)
270+   -  Send an email
271+   -  Send an email with a template
272+   -  Send a batch of emails
273+   -  Send a batch of emails with a template
274+   -  Messages management
275+   -  Inboxes management
276+   -  Projects management
277+   -  Attachments management
278+ -  Contacts
279+   -  Contacts management
280+   -  Contact Lists management
281+   -  Contact Fields management
282+   -  Contact Events management
283+   -  Contact Exports management
284+   -  Contact Imports management
285+ -  Suppressions
286+   -  Suppressions management (find and delete)
287+ -  Templates
288+   -  Templates management
289+ -  General
290+   -  Account access management
291+   -  Permissions management
292+   -  List accounts you have access to
293+   -  Get current billing information
294+ 
125295## Contributing  
126296
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 ) .
297+ 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 ) .
128298
129299### Development Environment  
130300
131301#### Clone the repo  
132302
133303``` bash 
134- https://github.com/railsware /mailtrap-python.git
304+ git clone  https://github.com/mailtrap /mailtrap-python.git
135305cd  mailtrap-python
136306``` 
137307
0 commit comments