Metadata-Version: 2.5
Name: tempmailo
Version: 0.1.0
Summary: Temporary email addresses for tests and apps: a small client for the TempMailo API v2.
Project-URL: Homepage, https://tempmailo.co/api/
Project-URL: Documentation, https://tempmailo.co/api/
Project-URL: Source, https://github.com/tempmailo-co/tempmailo-python
Project-URL: Issues, https://github.com/tempmailo-co/tempmailo-python/issues
Author: WOWLINK LTD
License-Expression: MIT
License-File: LICENSE
Keywords: api client,disposable email,email testing,temp mail,temporary email,webhooks
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Topic :: Communications :: Email
Classifier: Topic :: Software Development :: Testing
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown

# tempmailo

A small Python client for the [TempMailo API](https://tempmailo.co/api/): create temporary email addresses, read the mail they receive, and check the signature of webhook requests. No dependencies beyond the standard library, Python 3.10 or newer, MIT license.

Made for sign-up tests, checks of your own transactional email, and apps or bots that need a throwaway inbox. TempMailo addresses only receive mail; they cannot send.

## Install

```sh
pip install tempmailo
```

## API key

Create a free account at [tempmailo.co](https://tempmailo.co/signup/), confirm its email and create a key on the API page of your account. Pass it to `TempMailo(...)` or set `TEMPMAILO_API_KEY`. Keep the key out of public code.

The free limits are on the [API page](https://tempmailo.co/api/#limits): 100 new addresses a day, 60 requests a minute, addresses that live 10 or 60 minutes, webhooks up to 1000 events a day.

## Read a confirmation code

```python
import re
from tempmailo import TempMailo

mail = TempMailo()  # reads TEMPMAILO_API_KEY
box = mail.create(lifetime=600)
print("Sign up with", box.email)

# ... your test registers a user with box.email here ...

letter = mail.wait_for_message(box.token, timeout=120, match=lambda m: "code" in m.subject.lower())
code = re.search(r"\b\d{6}\b", letter.plain).group()
print("Code:", code)

mail.delete(box.token)
```

`wait_for_message` polls every 5 seconds by default, which keeps you far from the limit of 60 requests a minute.

## Methods

| Method | Returns |
| --- | --- |
| `create(lifetime=3600, domain=None, login=None, client=None)` | `Mailbox` with `email`, `expired` and the `token` that opens it |
| `status(token)` | `Status`: `mailbox`, `active`, `count`, `unread` (works after the address expires) |
| `messages(token, after=None)` | list of `MessageSummary`, newest first, up to 100 |
| `message(token, id)` | `Message`: `plain`, `html`, `to`, `cc`, `reply_to`, `attachments`; marks it read |
| `source(token, id)` | the raw message as `str` |
| `attachment(token, id, index)` | the file as `bytes` |
| `delete(token)` | deletes the address and its mail |
| `wait_for_message(token, timeout=120, interval=5, match=None)` | the first new `Message` that passes `match` |
| `domains()` | domains you may pass to `create`, lifetimes, login rules |
| `usage()` | your plan, limits and how much is used |
| `webhook()`, `set_webhook(url)`, `test_webhook()`, `rotate_webhook_secret()`, `delete_webhook()` | your account webhook |

Message objects call the sender `sender` and `sender_name` (the API field `from` is a reserved word in Python). Every object keeps the full JSON it came from in `raw`.

## Errors

Every failed call raises `TempMailoError` with the API `code` and the HTTP `status`. Subclasses: `AuthError` (401, 403), `ExpiredError` (410), `NotFoundError` (404) and `RateLimitError` (429, with `scope`, `limit`, `reset` and `retry_after`).

When the API asks to wait 60 seconds or less, the client waits and retries by itself (twice by default; see `max_retries` and `max_retry_wait`). A used-up daily limit is raised at once. `client.last_rate_limit` holds the last `X-RateLimit-*` headers.

## Webhooks

Set an HTTPS address once, and every email to addresses made with your key comes to it as a signed POST request:

```python
created = mail.set_webhook("https://example.com/hooks/mail")
secret = created["secret"]  # shown only once: store it
```

Check each request with the raw body, before you parse it:

```python
import os

from flask import Flask, request
from tempmailo import WebhookVerificationError, verify_webhook

SECRET = os.environ["TEMPMAILO_WEBHOOK_SECRET"]
app = Flask(__name__)


@app.post("/hooks/mail")
def hook():
    try:
        # the raw bytes; in FastAPI use: await request.body()
        event = verify_webhook(SECRET, request.headers, request.get_data())
    except WebhookVerificationError:
        return "", 400
    if event.type == "message.received":
        print(event.mailbox.email, event.message.subject)
    return "", 204
```

The signature follows [Standard Webhooks](https://www.standardwebhooks.com). Requests older or newer than 5 minutes are refused. The same email can come twice, so skip repeats by `event.id`. A full receiver on the standard library is in [examples/webhook_receiver.py](examples/webhook_receiver.py).

## Development

```sh
pip install -e . pytest ruff mypy
pytest
ruff check . && mypy
```

The tests run against a local stand-in of the API and never call tempmailo.co.

## License

MIT, see [LICENSE](LICENSE). Run by WOWLINK LTD, Cyprus. Questions: [tempmailo.co/contact](https://tempmailo.co/contact/).
