Metadata-Version: 2.4
Name: forktex-mail
Version: 1.0.0
Summary: Send and receive e-mail from any Python process: SMTP to send, IMAP to file and read, errors classified by the protocol.
License-Expression: AGPL-3.0-or-later OR LicenseRef-ForkTex-Commercial
License-File: LICENSE
License-File: NOTICE
Keywords: email,smtp,imap,mail,async
Author: FORKTEX
Author-email: info@forktex.com
Requires-Python: >=3.14,<4.0
Classifier: Development Status :: 5 - Production/Stable
Classifier: Framework :: AsyncIO
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Communications :: Email
Classifier: Typing :: Typed
Requires-Dist: aiosmtplib (>=5.1.3,<5.2)
Requires-Dist: email-validator (>=2.2,<3)
Requires-Dist: forktex (>=1.2,<2)
Requires-Dist: pydantic (>=2.14,<3)
Project-URL: Bug Tracker, https://github.com/forktex/forktex-mail/issues
Project-URL: Changelog, https://github.com/forktex/forktex-mail/blob/master/CHANGELOG.md
Project-URL: Documentation, https://github.com/forktex/forktex-mail/tree/master/docs
Project-URL: Homepage, https://forktex.com
Project-URL: Repository, https://github.com/forktex/forktex-mail
Description-Content-Type: text/markdown

<p align="center">
  <img src="https://raw.githubusercontent.com/forktex/forktex-mail/master/docs/assets/mail-banner.png" alt="forktex-mail" height="120">
</p>

<p align="center">
  <a href="https://pypi.org/project/forktex-mail/"><img src="https://img.shields.io/pypi/v/forktex-mail.svg" alt="PyPI"></a>
  <a href="https://pypi.org/project/forktex-mail/"><img src="https://img.shields.io/pypi/pyversions/forktex-mail.svg" alt="Python"></a>
  <a href="https://github.com/forktex/forktex-mail/blob/master/LICENSE"><img src="https://img.shields.io/pypi/l/forktex-mail.svg" alt="License"></a>
</p>

Send and process e-mail from any Python process — a one-off script, a worker or
a production API. SMTP sends, one connection per message or one session for
many; IMAP reads a mailbox incrementally, files, marks, moves and deletes. Every
failure says who has to act and whether to retry, decided from the codes SMTP
and IMAP define, never from a provider's wording.

```bash
pip install forktex-mail
```

```python
from forktex_mail import Mailbox, compose, fetch, send

box = Mailbox(host="mail.example.com", user="me@example.com", password="…", sender_name="Me")

sent = await send(box, compose(box, to="Ana <ana@example.com>", subject="Hello", text="Hi!", html="<p>Hi!</p>"))
inbox = await fetch(box, unseen=True)
```

## Requirements

| | |
|---|---|
| **Python** | 3.14 |
| **A mail server** | Any SMTP submission server to send; IMAP to read, file and process. Battle-tested against Zoho Mail, Postfix and Dovecot, and GreenMail. |
| **Sign-in** | A password (or an app password), or an OAuth2 access token for providers that require one (Microsoft 365, Outlook.com, Gmail). |
| **TLS** | Implicit TLS or STARTTLS, verified against the system's authorities or a private one you supply. Plain SMTP only for a local catcher, without credentials. |

## What you can build

| You want to | Read |
|---|---|
| Describe a mailbox once, from settings or a database row | [A mailbox](https://github.com/forktex/forktex-mail/blob/master/docs/guide.md#a-mailbox) |
| Send from a script, an API or a worker | [Send from a script](https://github.com/forktex/forktex-mail/blob/master/docs/guide.md#send-from-a-script), [Send from an API](https://github.com/forktex/forktex-mail/blob/master/docs/guide.md#send-from-an-api) |
| Send many messages on one logged-in connection | [Send many messages](https://github.com/forktex/forktex-mail/blob/master/docs/guide.md#send-many-messages) |
| Attach files, thread replies, add `List-Unsubscribe` | [Attachments, threads and headers](https://github.com/forktex/forktex-mail/blob/master/docs/guide.md#attachments-threads-and-headers) |
| Stay under a host's sending caps | [Stay under a sending cap](https://github.com/forktex/forktex-mail/blob/master/docs/guide.md#stay-under-a-sending-cap) |
| Sign in to Microsoft 365, Outlook.com or Gmail with OAuth2 | [Sign in with OAuth2](https://github.com/forktex/forktex-mail/blob/master/docs/guide.md#sign-in-with-oauth2) |
| Read a mailbox, read on from where you stopped, or read a whole folder in batches | [Read a mailbox](https://github.com/forktex/forktex-mail/blob/master/docs/guide.md#read-a-mailbox-and-read-on-later) |
| File sent mail, mark, move and delete — process an inbox | [Process an inbox](https://github.com/forktex/forktex-mail/blob/master/docs/guide.md#process-an-inbox) |
| Find out which messages bounced, and why | [Read bounces](https://github.com/forktex/forktex-mail/blob/master/docs/guide.md#read-bounces) |
| Decide whether to retry, fix settings or drop a message | [Decide what to do with a failure](https://github.com/forktex/forktex-mail/blob/master/docs/guide.md#decide-what-to-do-with-a-failure) |
| Test your own code that sends mail, without a server | [Test code that sends mail](https://github.com/forktex/forktex-mail/blob/master/docs/guide.md#test-code-that-sends-mail) |

## How it works

| Concept | What it is |
|---|---|
| **Mailbox** | One account's settings: hosts, sign-in, sender, TLS. Pure data; never reads the environment. |
| **Transport** | Anything that sends a message, says what the server did, and closes: `Smtp`, or `Capture` in tests. |
| **Session** | `async with Smtp(mailbox)`: one login for many messages, checked before each and reconnected when gone. |
| **Received** | A message read back: who it is `from_` and to, text, HTML, attachments, every header, the server's flags and arrival time, and — for a delivery report — its `bounce`. Reading never fails on a message. |
| **UID and UIDVALIDITY** | A message's identity in a folder. Keep both, read on with `after_uid` (or read a whole folder with `messages`), act on messages with `mark`, `move`, `delete`. |
| **MailError** | Every failure, with a `kind` that says who has to act: `invalid`, `config`, `auth`, `tls`, `unreachable`, `deferred`, `rejected`, or `unknown` when no usable answer came after the whole message was sent. |

## Things to know

| | |
|---|---|
| **Acceptance is not delivery** | `Sent` is what the submission server took. Unknown recipients often come back later as a bounce; read them with `fetch` and match `bounce.original_message_id`. |
| **The TLS mode follows the port** | Implicit TLS on 465 and 993, STARTTLS elsewhere, unless `security` (SMTP) and `imap_security` (IMAP) — set independently — say otherwise. A port spoken to in the wrong mode fails at once as `tls`, naming the setting. |
| **Results are JSON-ready** | `Received`, `Sent`, `Fetched` and the rest are frozen values whose JSON keys are camelCase (`messageId`, `receivedAt`; the author is `"from"`), for storing or queueing; Python code uses the field names. |
| **Nothing is sent twice by the library** | A session checks its connection before a message and never retries after one; retrying is the caller's decision, guided by `transient`. |
| **Changes need the folder's UIDVALIDITY** | `mark`, `move` and `delete` refuse UIDs from a folder that has since been rebuilt. |
| **Providers differ, the model does not** | Relays and mailboxes, a replaced Message-ID, Gmail's delete-means-archive, folders under `INBOX.` — each is handled or documented in [Operating](https://github.com/forktex/forktex-mail/blob/master/docs/operating.md#where-providers-differ). |

## Documentation

| Page | For |
|---|---|
| [Guide](https://github.com/forktex/forktex-mail/blob/master/docs/guide.md) | Runnable recipes for each way to use it |
| [Reference](https://github.com/forktex/forktex-mail/blob/master/docs/reference.md) | Every public name, setting and error |
| [Operating](https://github.com/forktex/forktex-mail/blob/master/docs/operating.md) | Providers, TLS, sending caps, bounces, what to watch |

## Stability

From 1.0.0, semantic versioning covers every name in `forktex_mail.__all__`,
the eight error kinds and the `AppErrorCode` each maps to. Anything else —
module paths included — is internal. Changes are listed in the
[CHANGELOG](https://github.com/forktex/forktex-mail/blob/master/CHANGELOG.md).

## Contributing

Working on `forktex-mail` itself? Start with
[`docs/development.md`](https://github.com/forktex/forktex-mail/blob/master/docs/development.md). Security issues go to the
address in [SECURITY.md](https://github.com/forktex/forktex-mail/blob/master/SECURITY.md).

## Licence

AGPL-3.0-or-later, or a commercial licence from FORKTEX S.R.L. See
[LICENSE](https://github.com/forktex/forktex-mail/blob/master/LICENSE) and [NOTICE](https://github.com/forktex/forktex-mail/blob/master/NOTICE).

