Metadata-Version: 2.3
Name: notifyvia-email
Version: 1.0.0
Summary: The frontend Python library for notifyvia.email
Requires-Dist: requests>=2.34.2
Requires-Python: >=3.13
Description-Content-Type: text/markdown

# notifyvia-email

The frontend Python library for [notifyvia.email](https://notifyvia.email/docs).

## Installation

```bash
pip install notifyvia-email
```

or with uv:

```bash
uv add notifyvia-email
```

Requires Python 3.13+.

## Quick start

```python
from notifyvia_email.v3 import NotifyViaEmail, SendResult

client = NotifyViaEmail(api_key="your-api-key")

resp = client.send(
    to="you@example.com",
    subject="A message from your website",
    text="Do you have any strawberry cheesecake?",
)

if resp.ok:
    print("Email queued for delivery")
else:
    print(resp.result, resp.errors)
```

## API versions

Each API version has its own client with the same interface:

```python
from notifyvia_email.v1 import NotifyViaEmail  # POST /api/v1/send
from notifyvia_email.v2 import NotifyViaEmail  # POST /api/v2/send
from notifyvia_email.v3 import NotifyViaEmail  # POST /api/v3/send
```

v2 adds the `reply_to` parameter. v3 adds [Cap](https://trycap.dev) captcha verification
(see [Captcha (v3)](#captcha-v3)). Everything else is shared.

## Sending HTML

Provide `html`, `text`, or both. At least one is required, otherwise `send()` raises `ValueError`.

```python
resp = client.send(
    to="you@example.com",
    subject="Welcome!",
    html="<h1>Welcome</h1><p>Thanks for signing up.</p>",
    text="Welcome! Thanks for signing up.",
    reply_to="support@example.com",  # v2 and v3 only
)
```

## Spam checking and rate limiting

A typical contact form setup. The rate limiter needs the sender's IPv4 address.

```python
resp = client.send(
    to="owner@example.com",
    subject="New contact form submission",
    text=form_message,
    enable_spam_checker=True,
    enable_rate_limiter=True,
    limit=3,             # max requests...
    within_seconds=300,  # ...per 5 minutes, per IP
    ipv4_address=request.remote_addr,
)
```

## Captcha (v3)

The v3 client can have notifyvia.email verify a [Cap](https://trycap.dev) captcha token before
sending. Cap is self-hosted, so you need your own Cap instance with your website added to it.

Set the Cap secret key and siteverify endpoint once when creating the client:

```python
from notifyvia_email.v3 import NotifyViaEmail, SendResult

client = NotifyViaEmail(
    api_key="your-api-key",
    cap_secret_key=CAP_SECRET_KEY,
    cap_api_endpoint=CAP_API_ENDPOINT,  # https://cap.example.com/<site-key>/siteverify
)
```

If you don't pass them, the client reads the `CAP_SECRET_KEY` and `CAP_API_ENDPOINT` environment
variables:

```bash
export CAP_SECRET_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
export CAP_API_ENDPOINT="https://cap.example.com/0000000/siteverify"
```

```python
client = NotifyViaEmail(api_key="your-api-key")  # captcha enabled from the environment
```

`/siteverify` is added to the endpoint if you leave it off, so `https://cap.example.com/0000000`
also works.

Then pass the token from your frontend form (the `cap-token` field the Cap widget adds) to each
`send()` call:

```python
resp = client.send(
    to="owner@example.com",
    subject="New contact form submission",
    text=form_message,
    cap_token=request.form["cap-token"],
)

if resp.result is SendResult.CAPTCHA_VERIFICATION_FAILED:
    print("Captcha failed:", resp.errors)
```

Notes:

- The captcha is only enabled when **both** the secret key and the endpoint are set. Otherwise
  v3 sends without a captcha check, and any `cap_token` is ignored.
- notifyvia.email calls your Cap instance, so in Cap either turn off **Restrict allowed
  origins** (CORS) or add `notifyvia.email` to the allowed list. Otherwise every send fails
  with `CAPTCHA_VERIFICATION_FAILED`.
- `CAPTCHA_VERIFICATION_FAILED` means the token, the endpoint, or the secret key is invalid.
  `resp.errors` says which.
- Cap tokens are single-use, so get a new one for each submission.

## Choosing a service

```python
resp = client.send(
    to="you@example.com",
    subject="Hello",
    text="Sent via Postmark",
    service="postmark",  # "zepto" (default) or "postmark"
)
```

## Handling the response

`send()` returns a `SendResponse` with:

- `result`: a `SendResult` enum member
- `errors`: the API's `errors` field (or `None`), e.g. per-field validation messages
- `ok`: `True` when the email was accepted for delivery

```python
from notifyvia_email.v3 import SendResult

resp = client.send(to="not-an-email", subject="Hi", text="Hello")

match resp.result:
    case SendResult.EMAIL_PROVISIONING:
        print("Sent")
    case SendResult.INVALID:
        print("Validation failed:", resp.errors)
        # {'to': ['Not a valid email address.']}
    case SendResult.RATE_LIMIT_EXCEEDED:
        print("Too many requests, try again later")
    case SendResult.SPAM_DETECTED:
        print("Message rejected as spam")
    case SendResult.CAPTCHA_VERIFICATION_FAILED:
        print("Captcha failed:", resp.errors)
    case SendResult.CONNECTION_ERROR:
        print("Could not reach notifyvia.email:", resp.errors)
    case _:
        print("Failed:", resp.result.name, resp.errors)
```

| `SendResult`                | HTTP | Meaning                                          |
|-----------------------------|------|--------------------------------------------------|
| `EMAIL_PROVISIONING`        | 200  | Accepted and queued for delivery                 |
| `AUTHORIZATION_REQUIRED`    | 401  | Missing or invalid API key                       |
| `RATE_LIMIT_EXCEEDED`       | 429  | Too many requests from this IP in the window     |
| `INVALID`                   | 460  | Request validation failed (see `errors`)         |
| `INVALID_ACCOUNT`           | 461  | API key accepted but no matching account         |
| `INVALID_SERVICE`           | 462  | `service` is not a supported provider            |
| `INSUFFICIENT_CREDITS`      | 463  | Not enough credits to send                       |
| `IPV4_ADDRESS_NOT_PROVIDED` | 464  | Rate limiter enabled without `ipv4_address`      |
| `SPAM_DETECTED`             | 465  | Body matched the spam dictionary                 |
| `SENDER_IPV4_BLOCKED`       | 466  | Sender IP is on the global block list            |
| `CAPTCHA_VERIFICATION_FAILED` | 467 | Captcha token, endpoint, or secret key invalid (v3) |
| `UNKNOWN_ERROR`             | -    | Unexpected status code from the API              |
| `CONNECTION_ERROR`          | -    | Network failure or timeout; `errors` has details |

## Configuration

```python
client = NotifyViaEmail(api_key="your-api-key", timeout=5.0)  # seconds, default 10
```

See the [notifyvia.email docs](https://notifyvia.email/docs) for full API details.
