Skip to content
level12Public

About

A simple mail library for Keg

Topics

Resources

Stars

0 stars

Watchers

8 watching

Forks

Latest commit

 

History

50 Commits

Folders and files

Repository files navigation

Keg-Mail

nox codecov

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

Usage

pip install keg-mail

Initialize Keg-Mail in your application:

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:

import keg_mail

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

Validate a recipient before sending:

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:

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:

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.

Development

Copier template

Project structure and tooling mostly derive from Coppy. See its documentation for context and additional instructions. Update this project by following Coppy's template update instructions.

Project setup

  1. Install the host dependencies.
  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.

About

A simple mail library for Keg

Topics

Resources

Stars

0 stars

Watchers

8 watching

Forks

Releases

Packages

Contributors

Languages