# 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.
## Quick links
- [Method Documentation](./docs)
- [PyPI Package](https://pypi.org/project/mailslurp-client/)
- [Github Source](https://github.com/mailslurp/mailslurp-client-python)
## Get started
::: tip
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](./docs)
:::
### 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`.
### Configure
Once installed you can import `mailslurp_client` and create a configuration 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-here"
```
## Common usage
MailSlurp can be used to create email addresses than can send and receive real emails and attachments in Python.
### Create an email address
Use `with` statements to create controllers for each endpoint of the MailSlurp API.
```python
def create_inbox_example():
with mailslurp_client.ApiClient(configuration) as api_client:
# create an inbox using the inbox controller
api_instance = mailslurp_client.InboxControllerApi(api_client)
inbox = api_instance.create_inbox()
# check the id and email_address of the inbox
assert len(inbox.id) > 0
assert "mailslurp.com" in inbox.email_address
```
### List inboxes
```python
def test_can_list_inboxes(self):
# use the with keyword to create an api client
with mailslurp_client.ApiClient(configuration) as api_client:
# create an inbox using the inbox controller
inbox_controller = mailslurp_client.InboxControllerApi(api_client)
inboxes = inbox_controller.get_all_inboxes(page=0)
# pagination properties
assert inboxes.total_pages > 0
assert inboxes.total_elements > 0
# view contents
assert len(inboxes.content[0].id) > 0
```
### Send emails
```python
def send_email_example():
with mailslurp_client.ApiClient(configuration) as api_client:
# first create an inbox
api_instance = mailslurp_client.InboxControllerApi(api_client)
inbox = api_instance.create_inbox()
# send email from the inbox
send_email_options = mailslurp_client.SendEmailOptions()
send_email_options.to = [inbox.email_address]
send_email_options.subject = "Hello"
send_email_options.body = "Your message"
send_email_options.is_html = True
api_instance.send_email(inbox.id, send_email_options=send_email_options)
```
### Use attachments
To send attachments first use the `AttachmentControllerApi` to upload a file or byte stream. Then use the attachment ID returned with subsequent send calls.
```python
@staticmethod
def upload_attachment():
with mailslurp_client.ApiClient(configuration) as api_client:
import base64
file_bytes = "Your file's bytes".encode("utf-8")
encoded_contents = base64.b64encode(file_bytes)
attachment_controller = mailslurp_client.AttachmentControllerApi(api_client)
upload_options = mailslurp_client.UploadAttachmentOptions(
content_type="text/plain",
filename="test.txt",
base64_contents=str(encoded_contents, 'utf-8')
)
print("upload_options = {}".format(upload_options))
return attachment_controller.upload_attachment(upload_options)
```
### Receive emails and extract content
```python
def receive_email_and_extract_content_example():
with mailslurp_client.ApiClient(configuration) as api_client:
# create two inboxes for testing
inbox_controller = mailslurp_client.InboxControllerApi(api_client)
inbox_1 = inbox_controller.create_inbox()
inbox_2 = inbox_controller.create_inbox()
# send email from inbox 1 to inbox 2
send_email_options = mailslurp_client.SendEmailOptions()
send_email_options.to = [inbox_2.email_address]
send_email_options.subject = "Hello inbox 2"
send_email_options.body = "Your code is: 123"
send_email_options.attachments = self.upload_attachment() # see previous section
inbox_controller.send_email(inbox_1.id, send_email_options=send_email_options)
# receive email for inbox 2
waitfor_controller = mailslurp_client.WaitForControllerApi(api_client)
email = waitfor_controller.wait_for_latest_email(inbox_id=inbox_2.id, timeout=30000, unread_only=True)
assert email.subject == "Hello inbox 2"
# extract content from body
import re
pattern = re.compile('Your code is: ([0-9]{3})')
matches = pattern.match(email.body)
code = matches.group(1)
assert code == "123"
```
### Download attachments
```python
def receive_email_and_extract_content_example():
with mailslurp_client.ApiClient(configuration) as api_client:
# wait for email to be received by inbox 2
email = wait_controller.wait_for_latest_email(inbox_id=inbox_2.id, timeout=30000, unread_only=True)
# assert that the message was received
assert email.inbox_id == inbox_2.id
assert email.subject == "Hello"
# attachment ids pressent
assert len(email.attachments) == 1
# download attachment content
email_controller = mailslurp_client.EmailControllerApi(api_client)
downloaded_bytes = email_controller.download_attachment(email.attachments[0], email.id)
assert len(downloaded_bytes) > 0
# alternatively download as base64 string with more meta data
email_controller = mailslurp_client.EmailControllerApi(api_client)
downloaded_attachment = email_controller.download_attachment_base64(email.attachments[0], email.id)
assert downloaded_attachment.content_type == "text/plain"
base64_contents = downloaded_attachment.base64_file_contents
import base64
attachment_bytes = base64.b64decode(base64_contents)
assert len(attachment_bytes) > 0
```
### 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://www.github.com/mailslurp/examples) or the [Method Documentation](./docs) for full usage.
没有合适的资源?快使用搜索试试~ 我知道了~
PyPI 官网下载 | mailslurp-client-9.0.0.tar.gz
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 178 浏览量
2022-01-13
02:14:42
上传
评论
收藏 106KB GZ 举报
温馨提示
共212个文件
py:204个
txt:4个
pkg-info:2个
资源来自pypi官网。 资源全名:mailslurp-client-9.0.0.tar.gz
资源推荐
资源详情
资源评论
收起资源包目录
PyPI 官网下载 | mailslurp-client-9.0.0.tar.gz (212个子文件)
setup.cfg 69B
README.md 7KB
PKG-INFO 9KB
PKG-INFO 9KB
inbox_controller_api.py 112KB
email_controller_api.py 91KB
alias_controller_api.py 52KB
group_controller_api.py 48KB
wait_for_controller_api.py 39KB
webhook_controller_api.py 33KB
api_client.py 26KB
template_controller_api.py 25KB
contact_controller_api.py 25KB
attachment_controller_api.py 22KB
mail_server_controller_api.py 22KB
common_actions_controller_api.py 21KB
domain_controller_api.py 20KB
send_email_options.py 19KB
email.py 16KB
bulk_actions_controller_api.py 16KB
configuration.py 13KB
create_inbox_dto.py 13KB
sent_email_dto.py 13KB
rest.py 12KB
inbox.py 12KB
form_controller_api.py 12KB
sent_emails_controller_api.py 11KB
sent_email_projection.py 11KB
page_expired_inbox_record_projection.py 11KB
reply_to_email_options.py 11KB
thread_projection.py 11KB
wait_for_conditions.py 11KB
alias_dto.py 11KB
webhook_dto.py 11KB
domain_dto.py 11KB
page_sent_email_projection.py 11KB
page_template_projection.py 11KB
page_webhook_projection.py 11KB
page_contact_projection.py 11KB
email_projection.py 10KB
page_thread_projection.py 10KB
page_inbox_projection.py 10KB
page_group_projection.py 10KB
page_email_projection.py 10KB
page_email_preview.py 10KB
contact_dto.py 10KB
page_alias.py 10KB
email_preview.py 10KB
reply_to_alias_email_options.py 10KB
create_contact_options.py 9KB
alias_projection.py 9KB
contact_projection.py 9KB
alias.py 9KB
__init__.py 8KB
update_inbox_options.py 8KB
setup.py 8KB
create_alias_options.py 8KB
webhook_projection.py 8KB
email_analysis.py 7KB
inbox_projection.py 7KB
domain_name_record.py 7KB
email_verification_result.py 7KB
dns_lookup_result.py 7KB
template_projection.py 7KB
dns_lookup_options.py 7KB
template_dto.py 7KB
__init__.py 7KB
pageable.py 7KB
expired_inbox_record_projection.py 6KB
verify_email_address_options.py 6KB
webhook_test_request.py 6KB
match_option.py 6KB
name_server_record.py 6KB
simple_send_email_options.py 6KB
upload_attachment_options.py 6KB
attachment_meta_data.py 6KB
group_projection.py 6KB
group_dto.py 6KB
download_attachment_dto.py 6KB
forward_email_options.py 6KB
describe_mail_server_domain_result.py 6KB
html_validation_result.py 5KB
create_webhook_options.py 5KB
expired_inbox_dto.py 5KB
webhook_test_result.py 5KB
domain_preview.py 5KB
template_variable.py 5KB
bulk_send_email_options.py 5KB
sort.py 5KB
email_content_match_result.py 5KB
content_match_options.py 5KB
basic_auth_options.py 5KB
group_contacts_dto.py 5KB
ip_address_result.py 5KB
create_template_options.py 5KB
validation_message.py 5KB
create_group_options.py 4KB
webhook_test_response.py 4KB
validation_dto.py 4KB
test_inbox_controller_api.py 4KB
共 212 条
- 1
- 2
- 3
资源评论
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功