Skip to main content

mailbox-client

CI Coverage License Python

PyPI benethos-mailbox-client

Sponsor PayPal

Beta, version 0.7.1. Usable with real accounts. A breaking change of the API or the configuration is announced in the changelog. Stored data is carried forward by migrations.

The Python client of the REST API of mailbox-service, on PyPI as benethos-mailbox-client. It covers the whole REST API: it reads, searches, sorts, drafts and sends mail of the accounts the service holds, and administers the service: accounts, users, roles, tokens, second factors, webhooks, the audit and the state of the service, as far as its token allows. Every operation of the API is a method of both clients, named like its operationId. The MCP server of the project, mailbox-mcp, is built on it.

It knows the service through its REST API alone, and needs nothing but httpx. The REST API follows the status above. This package's Python interface has no stability promise yet.

What the project is for: the repository's README.

Contents

Install

uv add benethos-mailbox-client      # or: pip install benethos-mailbox-client

Use

Make a token on your user's page in the service's UI. The client reads it from MAILBOX_SERVICE_TOKEN, and the address of the service from MAILBOX_SERVICE_URL (default http://127.0.0.1:8080). Both can be passed as arguments instead. The address carries no user or password: it is named in the errors of the client.

For async code:

from benethos_mailbox_client import MailboxClient

async with MailboxClient() as mailbox:
    me = await mailbox.get_me()
    for account in me.accounts:
        page = await mailbox.list_messages(account.id, folder="inbox", limit=10)
        for summary in page.items:
            print(account.email, summary.sender, summary.subject)

For code without an event loop, SyncMailboxClient has the same methods (close() for aclose()) and answers the same records:

from benethos_mailbox_client import SyncMailboxClient, message_body

with SyncMailboxClient() as mailbox:
    body = message_body(
        to=[("someone@example.org", None)],
        cc=[],
        bcc=[],
        subject="Hello",
        text="Hi there",
        html=None,
        reference=None,
    )
    mailbox.send_message("acc_...", body, idempotency_key="hello-1")

Administering the service works the same way. A user with a grant and a token for it:

from benethos_mailbox_client import Grant, SyncMailboxClient

with SyncMailboxClient() as mailbox:
    user = mailbox.create_user(
        "desktop",
        grants=[Grant(accounts=("acc_...",), allow=("mail.read", "drafts"))],
    )
    made = mailbox.create_token(user.id, "laptop")
    print(made.secret.get_secret_value())  # shown this once

A message comes as a Message, a message in a page, a changed one and a written draft as a MessageSummary, its sender and recipients as Address records.

A secret the service shows once, a new token, a one-time password or a webhook's signing secret, comes as a Secret: repr and str show stars, so it reaches no log by accident, and get_secret_value() reads it. A list of messages or drafts answers a Page of summaries, with next_cursor for the next page and, across accounts, not_answering for those that did not answer, as text, and incomplete, the same accounts as NotAnswering records with the error's code and message. The other lists that page, of accounts, threads, users, sends and the audit, answer Paged, with next_cursor as well.

Beside the operations, update_messages and trash_messages are batch_messages for one kind of change, and message_body builds the body of a send or a draft, as in the example above.

request(method, path, ...) sends any request and answers its JSON as it comes, e.g. for a field a record leaves out. The routes and their fields: docs/openapi.json.

The token travels in a header. Over http the client talks only to this machine. To another machine it needs https, or MAILBOX_SERVICE_ALLOW_HTTP=1 (allow_http=True) for a network you trust, such as between containers.

A client made without an address or a token reads the environment when it is made. A program that makes clients for a long time can read it once, with from_environment():

from benethos_mailbox_client import MailboxClient, from_environment

found = from_environment()
client = MailboxClient(found.url, found.token, allow_http=found.allow_http)

Errors

Everything the client raises is a MailboxError:

Error When
ConfigurationError no token, or an address it refuses
ServiceUnavailableError the service cannot be reached
ServiceTimeoutError the service took the request but did not answer in time
ApiError the API answered with an error: status, code, message, and retry_after, the seconds a limit asks to wait, else None. An answer of another shape has the code unexpected_response. So has one that cannot be read, with the status 502

Inside

Each endpoint is described once, in endpoints/, one module per resource of the API: its method, path, query, body and how its answer becomes a record of models/. That package sends nothing. MailboxClient sends those requests with httpx.AsyncClient, SyncMailboxClient with httpx.Client, and a method of either is one line made from its endpoint, with the endpoint's name, docstring and signature, get_attachment, which streams, a few more. A test holds every operation of the API to a method of both clients, except the route of a link to an attachment, whose URL another service fetches without a token. What both share stands beside them: the address and the token (environment.py), the shape of a request (calls.py), how an answer or a failure is read (answers.py) and an attachment read in chunks (attachments.py).

License

MIT, see LICENSE.

Metadata

Release files for benethos-mailbox-client 0.7.1

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

Source distribution (sdist)

Source distribution for benethos-mailbox-client 0.7.1
File Size Uploaded Provenance Actions
benethos_mailbox_client-0.7.1.tar.gz 51.9 kB Without provenance Details for benethos_mailbox_client-0.7.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for benethos-mailbox-client 0.7.1
File Interpreter ABI Platform Actions
benethos_mailbox_client-0.7.1-py3-none-any.whl Python 3 none any Details for benethos_mailbox_client-0.7.1-py3-none-any.whl

Total release size: 108.3 kB

Release files / benethos_mailbox_client-0.7.1.tar.gz

Download URL benethos_mailbox_client-0.7.1.tar.gz
Size 51.9 kB
Tags Source
SHA-256 checksum
How to use checksums
dbee6a0059e06758225ab291cf16e579b468132b4994d0f7d6d984869fe0a87a
BLAKE2b-256 checksum
How to use checksums
047ca2b04511209aca6042b8c4ed19a39b13f5313d6d6efc026e045b5b209513
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / benethos_mailbox_client-0.7.1-py3-none-any.whl

Download URL benethos_mailbox_client-0.7.1-py3-none-any.whl
Size 56.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a5ec97fe15aa2393f796bc0985a6af6db757f3bc4d0085191289c3db9aeb7cf6
BLAKE2b-256 checksum
How to use checksums
4768a953e2202533a7eb21e2bea1c304c30c1e9763539a3d6fc8ae4f2e094e5f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for benethos-mailbox-client 0.7.1

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.

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