Metadata-Version: 2.5
Name: Keg-Mail
Version: 0.20261119.1
Summary: A mail sending library for keg applications
Project-URL: Homepage, https://github.com/level12/keg-mail
Project-URL: Repository, https://github.com/level12/keg-mail
Author-email: Level 12 Developers <devteam@level12.io>
License-Expression: BSD-3-Clause
License-File: license.txt
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: BSD License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Requires-Python: >=3.12
Requires-Dist: arrow
Requires-Dist: email-validator>=2
Requires-Dist: flask-mail>=0.10.0
Requires-Dist: keg
Requires-Dist: pymailcheck
Requires-Dist: sqlalchemy-utils
Provides-Extra: mailgun
Requires-Dist: ordered-set; extra == 'mailgun'
Requires-Dist: requests; extra == 'mailgun'
Provides-Extra: test
Requires-Dist: flask-webtest; extra == 'test'
Requires-Dist: kegelements; extra == 'test'
Requires-Dist: nox; extra == 'test'
Requires-Dist: pytest; extra == 'test'
Requires-Dist: pytest-cov; extra == 'test'
Requires-Dist: requests-mock; extra == 'test'
Description-Content-Type: text/markdown

# Keg-Mail

[![nox](https://github.com/level12/keg-mail/actions/workflows/nox.yaml/badge.svg)](https://github.com/level12/keg-mail/actions/workflows/nox.yaml)

Keg-Mail is a basic wrapper around Flask-Mail which adds support for email
templates and Mailgun integrations.


## Usage

```shell
pip install keg-mail
```

Initialize Keg-Mail in your application:

```python
import flask
from keg import Keg
from keg.signals import app_ready
import keg_mail

bp = flask.Blueprint('main', __name__)
mail = keg_mail.KegMail()


class App(Keg):
    use_blueprints = [bp]


@app_ready.connect
def init_extensions(app):
    """Initialize custom extensions used by this application."""
    mail.init_app(app)
```

Define email content:

```python
import keg_mail

hello_world_content = keg_mail.EmailContent(
    text='Hello {name}!',
    html='<h1>Hello {name}!</h1>',
)
```

Validate a recipient before sending:

```python
import keg_mail

address = 'user@example.com'
if keg_mail.Email.address_might_be_invalid(address):
    # The address is invalid or resembles a common typo.
    suggestion = keg_mail.Email.get_invalid_address_suggestions(address)
else:
    # The address has valid syntax and does not resemble a common typo.
    suggestion = None
```

Send the email:

```python
from app import mail
import app.emails as emails
import keg_mail


@bp.route('/')
def index():
    mail.send_email(
        'you@example.com',
        keg_mail.Email(
            subject='Hello {name}!',
            content=emails.hello_world_content,
        ).format(name='You'),
    )
```

Test the email:

```python
from app import mail


def test_send_mail():
    with mail.record_messages() as outbox:
        app.test_client.get('/')
        assert len(outbox) == 1
        assert outbox[0].subject == 'Hello You'
```


## Changelog


### 0.3.1 — 2024-06-21

- Resolve a missed signal signature change.


### 0.3.0 — 2024-06-21

- Automatically pass the reply-to header to Mailgun.
- Support Flask-Mail 0.10.0 signal changes.

Earlier release notes remain available in [changelog.rst](changelog.rst).


## Development


### Copier template

Project structure and tooling mostly derive from
[Coppy](https://github.com/level12/coppy). See its documentation for context and
additional instructions. Update this project by following Coppy's
[template update instructions](https://github.com/level12/coppy/wiki#updating-a-project).


### Project setup

1. Install the [host dependencies](https://github.com/level12/coppy/wiki/Mise).
2. Run `uv sync`.
3. Install the hooks with `prek install`.
4. Run the checks with `nox`.


### Versions

Use `mise run version show` to show the current version and
`mise run version bump` to create a version commit and tag.
