mailslurp-client


Namemailslurp-client JSON
Version 11.7.2 PyPI version JSON
download
home_pagehttps://www.mailslurp.com/docs/python
SummaryOfficial MailSlurp Python SDK Email API
upload_time2021-04-16 07:18:41
maintainer
docs_urlNone
authorMailSlurp
requires_python
license
keywords mailslurp email smtp mailer mailslurp api test
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# 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

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.    



            

Raw data

            {
    "_id": null,
    "home_page": "https://www.mailslurp.com/docs/python",
    "name": "mailslurp-client",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "MailSlurp,Email,SMTP,Mailer,MailSlurp API,Test",
    "author": "MailSlurp",
    "author_email": "support@mailslurp.zendesk.com",
    "download_url": "https://files.pythonhosted.org/packages/c3/7e/cf8dffbe2bf898cdb5ef10921bf0e22b76567cd6145e6bd1c7b9ec8e127f/mailslurp-client-11.7.2.tar.gz",
    "platform": "",
    "description": "\n# MailSlurp Python Client\n\n> Create real email addresses on demand. Send and receive emails and attachments from code and tests using Python.\n\nMailSlurp 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.\n\n## Quick links\n\n- [Method Documentation](./docs)\n- [PyPI Package](https://pypi.org/project/mailslurp-client/)\n- [Github Source](https://github.com/mailslurp/mailslurp-client-python)\n\n## Get started\n\nThis section describes how to get up and running with the Python client.\n\nSee the [examples page](https://www.mailslurp.com/examples/) for more examples and use with common frameworks such as Django, Flask and Pytest.\n\nSee the method documentation for a [list of all functions](./docs)\n\n### Create API Key\n\nFirst you'll need an API Key. [Create a free account](https://app.mailslurp.com) and copy the key from your dashboard.\n\n### Install package\n\nMailSlurp has an official PyPI package called `mailslurp-client`. It supports Python version 2 and 3.\n\n```bash\npip install mailslurp-client\n```\n\nOn some systems you may need to install `distutils`.\n\n### Configure\n\nOnce installed you can import `mailslurp_client` and create a configuration with your [API Key](https://app.mailslurp.com).\n\n```python\nimport mailslurp_client\n\nconfiguration = mailslurp_client.Configuration()\nconfiguration.api_key['x-api-key'] = \"your-api-key-here\"\n```\n\n## Common usage\n\nMailSlurp can be used to create email addresses than can send and receive real emails and attachments in Python.\n\n### Create an email address\n\nUse `with` statements to create controllers for each endpoint of the MailSlurp API.\n\n```python\ndef create_inbox_example():\n    with mailslurp_client.ApiClient(configuration) as api_client:\n\n        # create an inbox using the inbox controller\n        api_instance = mailslurp_client.InboxControllerApi(api_client)\n        inbox = api_instance.create_inbox()\n\n        # check the id and email_address of the inbox\n        assert len(inbox.id) > 0\n        assert \"mailslurp.com\" in inbox.email_address\n```\n\n### List inboxes\n\n```python\ndef test_can_list_inboxes(self):\n    # use the with keyword to create an api client\n    with mailslurp_client.ApiClient(configuration) as api_client:\n        # create an inbox using the inbox controller\n        inbox_controller = mailslurp_client.InboxControllerApi(api_client)\n        inboxes = inbox_controller.get_all_inboxes(page=0)\n\n        # pagination properties\n        assert inboxes.total_pages > 0\n        assert inboxes.total_elements > 0\n\n        # view contents\n        assert len(inboxes.content[0].id) > 0\n```\n\n### Send emails\n\n```python\ndef send_email_example():\n    with mailslurp_client.ApiClient(configuration) as api_client:\n        # first create an inbox\n        api_instance = mailslurp_client.InboxControllerApi(api_client)\n        inbox = api_instance.create_inbox()\n\n        # send email from the inbox\n        send_email_options = mailslurp_client.SendEmailOptions()\n        send_email_options.to = [inbox.email_address]\n        send_email_options.subject = \"Hello\"\n        send_email_options.body = \"Your message\"\n        send_email_options.is_html = True\n        api_instance.send_email(inbox.id, send_email_options=send_email_options)\n```\n\n### Use attachments\nTo send attachments first use the `AttachmentControllerApi` to upload a file or byte stream. Then use the attachment ID returned with subsequent send calls.\n\n```python\n@staticmethod\ndef upload_attachment():\n    with mailslurp_client.ApiClient(configuration) as api_client:\n        import base64\n\n        file_bytes = \"Your file's bytes\".encode(\"utf-8\")\n        encoded_contents = base64.b64encode(file_bytes)\n        attachment_controller = mailslurp_client.AttachmentControllerApi(api_client)\n\n        upload_options = mailslurp_client.UploadAttachmentOptions(\n            content_type=\"text/plain\",\n            filename=\"test.txt\",\n            base64_contents=str(encoded_contents, 'utf-8')\n        )\n        print(\"upload_options = {}\".format(upload_options))\n        return attachment_controller.upload_attachment(upload_options)\n```\n\n### Receive emails and extract content\n\n```python\ndef receive_email_and_extract_content_example():\n    with mailslurp_client.ApiClient(configuration) as api_client:\n        # create two inboxes for testing\n        inbox_controller = mailslurp_client.InboxControllerApi(api_client)\n        inbox_1 = inbox_controller.create_inbox()\n        inbox_2 = inbox_controller.create_inbox()\n\n        # send email from inbox 1 to inbox 2\n        send_email_options = mailslurp_client.SendEmailOptions()\n        send_email_options.to = [inbox_2.email_address]\n        send_email_options.subject = \"Hello inbox 2\"\n        send_email_options.body = \"Your code is: 123\"\n        send_email_options.attachments = self.upload_attachment() # see previous section\n\n        inbox_controller.send_email(inbox_1.id, send_email_options=send_email_options)\n\n        # receive email for inbox 2\n        waitfor_controller = mailslurp_client.WaitForControllerApi(api_client)\n        email = waitfor_controller.wait_for_latest_email(inbox_id=inbox_2.id, timeout=30000, unread_only=True)\n\n        assert email.subject == \"Hello inbox 2\"\n\n        # extract content from body\n        import re\n        pattern = re.compile('Your code is: ([0-9]{3})')\n        matches = pattern.match(email.body)\n        code = matches.group(1)\n\n        assert code == \"123\"\n```\n\n### Download attachments\n```python\n\ndef receive_email_and_extract_content_example():\n    with mailslurp_client.ApiClient(configuration) as api_client:\n        # wait for email to be received by inbox 2\n        email = wait_controller.wait_for_latest_email(inbox_id=inbox_2.id, timeout=30000, unread_only=True)\n\n        # assert that the message was received\n        assert email.inbox_id == inbox_2.id\n        assert email.subject == \"Hello\"\n\n        # attachment ids pressent\n        assert len(email.attachments) == 1\n\n        # download attachment content\n        email_controller = mailslurp_client.EmailControllerApi(api_client)\n        downloaded_bytes = email_controller.download_attachment(email.attachments[0], email.id)\n        assert len(downloaded_bytes) > 0\n\n        # alternatively download as base64 string with more meta data\n        email_controller = mailslurp_client.EmailControllerApi(api_client)\n        downloaded_attachment = email_controller.download_attachment_base64(email.attachments[0], email.id)\n        assert downloaded_attachment.content_type == \"text/plain\"\n        base64_contents = downloaded_attachment.base64_file_contents\n        import base64\n        attachment_bytes = base64.b64decode(base64_contents)\n        assert len(attachment_bytes) > 0\n```\n\n### Verify email address\nYou can verify email addresses with MailSlurp. This will perform SMTP queries for the email address on your behalf.\n\n```python\ndef test_validate_email(self):\n    with mailslurp_client.ApiClient(configuration) as api_client:\n        mailserver_controller = mailslurp_client.MailServerControllerApi(api_client)\n        verify_options = mailslurp_client.VerifyEmailAddressOptions(email_address=\"test@gmail.com\")\n        result = mailserver_controller.verify_email_address(verify_options=verify_options)\n        assert result.error is None\n        assert result.is_valid is True\n```\n\n## SDK Documentation\n\nSee 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.    \n\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Official MailSlurp Python SDK Email API",
    "version": "11.7.2",
    "split_keywords": [
        "mailslurp",
        "email",
        "smtp",
        "mailer",
        "mailslurp api",
        "test"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "e210d9fd9ef3d9a689ee81f188f9e10b",
                "sha256": "39139a5fa33fd04a084666e7226d3657a11a9659f0d2e9b8afb569d48f4edce0"
            },
            "downloads": -1,
            "filename": "mailslurp_client-11.7.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e210d9fd9ef3d9a689ee81f188f9e10b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 264303,
            "upload_time": "2021-04-16T07:18:40",
            "upload_time_iso_8601": "2021-04-16T07:18:40.137718Z",
            "url": "https://files.pythonhosted.org/packages/d4/4c/1a2fc7cfbdfb3a4b7c35da576941f825b68ccb98c55533df66d146b79cba/mailslurp_client-11.7.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "73b75f9b7366cd446e2e2dc98e129e14",
                "sha256": "05616eb5715d59942fcaf9e070151f856f4b8f28db22b895cf7d262a6f3e6129"
            },
            "downloads": -1,
            "filename": "mailslurp-client-11.7.2.tar.gz",
            "has_sig": false,
            "md5_digest": "73b75f9b7366cd446e2e2dc98e129e14",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 119761,
            "upload_time": "2021-04-16T07:18:41",
            "upload_time_iso_8601": "2021-04-16T07:18:41.676016Z",
            "url": "https://files.pythonhosted.org/packages/c3/7e/cf8dffbe2bf898cdb5ef10921bf0e22b76567cd6145e6bd1c7b9ec8e127f/mailslurp-client-11.7.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2021-04-16 07:18:41",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "mailslurp-client"
}
        
Elapsed time: 0.28522s