Metadata-Version: 2.3
Name: notifyvia-email
Version: 0.1.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.v2 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
```

v2 adds the `reply_to` parameter. 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 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,
)
```

## 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.v2 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.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            |
| `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.
