Skip to main content

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

Python email tutorial

Get started

This section describes how to get up and running with the Python client.

See the examples page for more examples and use with common frameworks such as Django, Flask and Pytest.

See the method documentation for a list of all functions or jump to common controllers below:

Create API Key

First you'll need an API Key. Create a free account 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.

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:

pip install python-certifi-win32

Configure

Once installed you can import mailslurp_client and create a configuration with your API Key.

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:

with mailslurp_client.ApiClient(configuration) as api_client:
    api_instance = mailslurp_client.InboxControllerApi(api_client)

See the controllers overview 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:

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:

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 for more methods.

Access inbox using SMTP

smtp_access = inbox_controller.get_imap_smtp_access(inbox_id=inbox.id)
self.assertIsNotNone(smtp_access.secure_smtp_server_host)

Send with SMTP client

# 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

This 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:

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

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

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.

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:

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 to wait for an expected email count to be satisfied and then return those emails.

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

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

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

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.

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 or the examples or the Method Documentation for full usage.

Metadata

Release files for mailslurp-client 17.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mailslurp-client 17.6.0
File Size Uploaded Provenance Actions
mailslurp_client-17.6.0.tar.gz 990.5 kB Without provenance Details for mailslurp_client-17.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mailslurp-client 17.6.0
File Interpreter ABI Platform Actions
mailslurp_client-17.6.0-py3-none-any.whl Python 3 none any Details for mailslurp_client-17.6.0-py3-none-any.whl

Total release size: 3.1 MB

Release files / mailslurp_client-17.6.0.tar.gz

Download URL mailslurp_client-17.6.0.tar.gz
Size 990.5 kB
Tags Source
SHA-256 checksum
How to use checksums
bb57cca2d3b183aa219cf04494e78509e2a7ec46747e9c968e95804f0e318121
BLAKE2b-256 checksum
How to use checksums
342dbc5117c75dda323530ef3e061df9cdd92d6b959cc8cbc5afd799f1efc0f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.6

Release files / mailslurp_client-17.6.0-py3-none-any.whl

Download URL mailslurp_client-17.6.0-py3-none-any.whl
Size 2.1 MB
Tags Python 3
SHA-256 checksum
How to use checksums
ba14a4504eeb42ec9b75de9975ed841e53fe3ea256b42da881c1accbc0d7ed41
BLAKE2b-256 checksum
How to use checksums
84aed8c86f4ed92c476b4ea22b7f44f713cd63f48ac24d6cc476d9f7f6a88c81
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.6

Security for mailslurp-client 17.6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

17.6.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page