Keg-Mail is a basic wrapper around Flask-Mail which adds support for email templates and Mailgun integrations.
pip install keg-mailInitialize 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 = NoneSend 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'- Resolve a missed signal signature change.
- Automatically pass the reply-to header to Mailgun.
- Support Flask-Mail 0.10.0 signal changes.
Earlier release notes remain available in changelog.rst.
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.
- Install the host dependencies.
- Run
uv sync. - Install the hooks with
prek install. - Run the checks with
nox.
Use mise run version show to show the current version and
mise run version bump to create a version commit and tag.