-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup.py
284 lines (215 loc) · 8.91 KB
/
setup.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
from setuptools import setup, find_packages
setup(
name="mailslurp-client",
version="15.19.22",
description="Official MailSlurp Python SDK Email API",
author="MailSlurp",
author_email="contact@mailslurp.dev",
url="https://www.mailslurp.com/python",
keywords=["MailSlurp", "Email", "SMTP", "Mailer", "MailSlurp API", "Test"],
install_requires=["urllib3 >= 1.15", "six >= 1.10", "certifi", "python-dateutil"],
packages=find_packages(exclude=["test", "tests"]),
include_package_data=True,
long_description="""
# MailSlurp Python Client
> Create real email addresses on demand. Send and receive emails and attachments from code and tests using Python.
MailSlurp is an email API service that lets you create real email addresses in code. You can then send and receive emails and attachments in Python applications and tests.
## Video tutorial
[](https://youtu.be/tcLJ3xX-H88)
## Quick links
- [API documentation](https://docs.mailslurp.com/api/)
- [Method Documentation](https://python.mailslurp.com/)
- [PyPI Package](https://pypi.org/project/mailslurp-client/)
- [Github Source](https://github.com/mailslurp/mailslurp-client-python)
- [Send email using SMTP in Python](https://www.mailslurp.com/smtp/python-send-email-smtp/)
- [SMTP access details](https://www.mailslurp.com/guides/smtp-imap/)
## Get started
This section describes how to get up and running with the Python client.
See the [examples page](https://www.mailslurp.com/examples/) for more examples and use with common frameworks such as Django, Flask and Pytest.
See the method documentation for a [list of all functions](https://python.mailslurp.com/) or jump to common controllers below:
- [InboxController](https://python.mailslurp.com/api/inbox_controller_api.html)
- [EmailController](https://python.mailslurp.com/api/email_controller_api.html)
- [SMSController](https://python.mailslurp.com/api/sms_controller_api.html)
- [WaitForController](https://python.mailslurp.com/api/wait_for_controller_api.html)
### Create API Key
First you'll need an API Key. [Create a free account](https://app.mailslurp.com) and copy the key from your dashboard.
### Install package
MailSlurp has an official PyPI package called `mailslurp-client`. It supports Python version 2 and 3.
```bash
pip install mailslurp-client
```
On some systems you may need to install `distutils`. If you encounter a `CERTIFICATE_VERIFY_VALID` error you can install `certifi` and `certifi-win32` for Windows:
```bash
pip install python-certifi-win32
```
### Configure
Once installed you can import `mailslurp_client` and create a [configuration](https://python.mailslurp.com/configuration.html) with your [API Key](https://app.mailslurp.com).
```python
import mailslurp_client
configuration = mailslurp_client.Configuration()
configuration.api_key["x-api-key"] = YOUR_API_KEY
```
Then you can create API controller instances using the configuration:
```python
with mailslurp_client.ApiClient(configuration) as api_client:
api_instance = mailslurp_client.InboxControllerApi(api_client)
```
See the [controllers overview](https://python.mailslurp.com/api/index.html) for all API methods.
## Email usage examples
MailSlurp can be used to create email addresses than can send and receive real emails, SMS, and attachments in Python.
### Create an email address
Create an inbox using the inbox controller:
```python
inbox_controller = mailslurp_client.InboxControllerApi(api_client)
inbox = inbox_controller.create_inbox_with_defaults()
self.assertTrue("@mailslurp" in inbox.email_address)
```
You can pass options using the `CreateInboxOptions` class:
```python
options = mailslurp_client.CreateInboxDto()
options.name = "Test inbox"
options.inbox_type = "SMTP_INBOX"
inbox = inbox_controller.create_inbox_with_options(options)
self.assertTrue("@mailslurp" in inbox.email_address)
```
See the [inbox controller](https://python.mailslurp.com/api/inbox_controller_api.html) for more methods.
### Access inbox using SMTP
```python
smtp_access = inbox_controller.get_imap_smtp_access(inbox_id=inbox.id)
self.assertIsNotNone(smtp_access.secure_smtp_server_host)
```
### Send with SMTP client
```python
# configure smtp client using access details
from smtplib import SMTP
with SMTP(
host=smtp_access.secure_smtp_server_host,
port=smtp_access.secure_smtp_server_port,
) as smtp:
msg = "Subject: Test subject\r\n\r\nThis is the body"
smtp.login(
user=smtp_access.secure_smtp_username,
password=smtp_access.secure_smtp_password,
)
smtp.sendmail(
from_addr=inbox.email_address,
to_addrs=[inbox.email_address],
msg=msg,
)
smtp.quit()
```
### List inboxes
List inboxes using the [inbox controller](https://python.mailslurp.com/api/inbox_controller_api.html):
```python
inboxes = inbox_controller.get_all_inboxes(page=0)
# pagination properties
self.assertTrue(inboxes.total_pages > 0)
self.assertTrue(inboxes.total_elements > 0)
# view contents
self.assertIsNotNone(inboxes.content[0].email_address)
```
### Get an inbox
```python
inbox = inbox_controller.get_inbox(inbox_id=inbox.id)
self.assertTrue("@mailslurp" in inbox.email_address)
# get by email address
inbox_by_email = inbox_controller.get_inbox_by_email_address(
inbox.email_address
)
self.assertTrue(inbox_by_email.exists)
# get by name
inbox_by_name = inbox_controller.get_inbox_by_name(inbox.name)
self.assertTrue(inbox_by_name.exists)
```
### Delete an inbox
```python
inbox_controller.delete_inbox(inbox_id=inbox.id)
```
### Upload attachments
To send attachments first upload the attachments as base64 encoded strings and use the returned attachment IDs when sending.
```python
import base64
attachment_controller = mailslurp_client.AttachmentControllerApi(api_client)
options = mailslurp_client.UploadAttachmentOptions(
filename="test.txt",
content_type="text/plain",
base64_contents=base64.b64encode("Hello world".encode("utf-8")).decode(
"utf-8"
),
)
attachment_ids = attachment_controller.upload_attachment(options)
self.assertTrue(len(attachment_ids) == 1)
```
### Send emails
Send emails with the [inbox controller](https://python.mailslurp.com/api/inbox_controller_api.html):
```python
send_options = mailslurp_client.SendEmailOptions(
to=[recipient.email_address],
subject="Hello",
body="Here is your email body",
attachments=attachment_ids,
)
sent = inbox_controller.send_email_and_confirm(
inbox_id=inbox.id, send_email_options=send_options
)
self.assertTrue(sent.sent_at is not None)
```
### Receive emails and extract content
Use the [wait for controller](https://python.mailslurp.com/api/wait_for_controller_api.html) to wait for an expected email count to be satisfied and then return those emails.
```python
wait_for_controller = mailslurp_client.WaitForControllerApi(api_client)
email = wait_for_controller.wait_for_latest_email(
inbox_id=inbox.id, timeout=60_000, unread_only=True
)
self.assertTrue("Hello" in email.subject)
```
### Email content matching
```python
matching_emails = wait_for_controller.wait_for_matching_emails(
inbox_id=inbox.id,
timeout=60_000,
unread_only=False,
match_options=mailslurp_client.MatchOptions(
conditions=[
mailslurp_client.ConditionOption(
condition="HAS_ATTACHMENTS", value="TRUE"
)
],
matches=[
mailslurp_client.MatchOption(
field="SUBJECT", should="CONTAIN", value="Hello"
)
],
),
count=1,
)
self.assertTrue(len(matching_emails) > 0)
```
### Download attachments
```python
attachment_content = attachment_controller.download_attachment_as_base64_encoded(email.attachments[0])
attachment_metadata = attachment_controller.get_attachment_info(email.attachments[0])
self.assertEqual(attachment_metadata.content_type, "text/plain")
```
### Fetch email by ID
```python
email_controller = mailslurp_client.EmailControllerApi(api_client)
email = email_controller.get_email(email_id=email_id)
self.assertTrue("Hello" in email.subject)
```
### Verify email address
You can verify email addresses with MailSlurp. This will perform SMTP queries for the email address on your behalf.
```python
def test_validate_email(self):
with mailslurp_client.ApiClient(configuration) as api_client:
mailserver_controller = mailslurp_client.MailServerControllerApi(api_client)
verify_options = mailslurp_client.VerifyEmailAddressOptions(email_address="test@gmail.com")
result = mailserver_controller.verify_email_address(verify_options=verify_options)
assert result.error is None
assert result.is_valid is True
```
## SDK Documentation
See the [guides page](https://www.mailslurp.com/guides/) or [the examples](https://github.com/mailslurp/examples) or the [Method Documentation](https://python.mailslurp.com/) for full usage.
""",
long_description_content_type="text/markdown"
)