aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg
diff options
context:
space:
mode:
authorDennis Fink2026-10-26 22:18:46 +0100
committerDennis Fink2026-10-26 22:18:46 +0100
commita67967275d0093f0ac3c3af85bf5396511c64422 (patch)
tree5c85f635200cec84414d09a0b1e36cc5e06e9635 /webmentions_ssg
parent2407e015bf8f1f67ed42b5622ae5095aa6477895 (diff)
downloadwebmentions-ssg-a67967275d0093f0ac3c3af85bf5396511c64422.tar.gz
webmentions-ssg-a67967275d0093f0ac3c3af85bf5396511c64422.zip
feat(core): add SMTP test mail command
Make outgoing mail configuration easier to validate during deployment without waiting for a Webmention notification to exercise the SMTP path. Allow operators to override the configured notification recipient and surface missing configuration or delivery failures directly through the CLI.
Diffstat (limited to '')
-rw-r--r--webmentions_ssg/__init__.py45
1 files changed, 43 insertions, 2 deletions
diff --git a/webmentions_ssg/__init__.py b/webmentions_ssg/__init__.py
index 1797a4d..11bd632 100644
--- a/webmentions_ssg/__init__.py
+++ b/webmentions_ssg/__init__.py
@@ -10,10 +10,10 @@ from pathlib import Path
import click
import coloredlogs
import sqlalchemy as sa
-from flask import Flask
+from flask import Flask, current_app
from flask.cli import AppGroup
from flask_bootstrap import Bootstrap5
-from flask_mail import Mail
+from flask_mail import Mail, Message
from flask_migrate import Migrate, upgrade
from flask_sqlalchemy import SQLAlchemy
from flask_wtf.csrf import CSRFProtect
@@ -40,6 +40,7 @@ MAIL = Mail()
USERS_CLI = AppGroup("users", help="Handle users")
DB_CLI = AppGroup("db", help="Handle database")
+MAIL_CLI = AppGroup("mail", help="Handle mail")
def create_app(config_class: type[DefaultConfig] | None = None) -> Flask:
@@ -130,6 +131,7 @@ def create_app(config_class: type[DefaultConfig] | None = None) -> Flask:
app.register_blueprint(root_page)
app.cli.add_command(USERS_CLI)
+ app.cli.add_command(MAIL_CLI)
return app
@@ -169,3 +171,42 @@ def create_db() -> None:
Upgrade the database to the latest migration revision.
"""
upgrade()
+
+
+@MAIL_CLI.command("test", help="Send an email to test the SMTP configuration")
+@click.option(
+ "--to", "recipient", help="Recipient address (defaults to NOTIFICATION_EMAIL)."
+)
+def send_test_mail(recipient: str | None) -> None:
+ """
+ Send a test email using the configured SMTP server.
+
+ :param recipient: Optional recipient overriding NOTIFICATION_EMAIL.
+ :raises click.ClickException: If configuration is missing or sending fails.
+ """
+ recipient = recipient or current_app.config.get("NOTIFICATION_EMAIL")
+ if not recipient:
+ raise click.ClickException(
+ "No recipient specified. Set NOTIFICATION_EMAIL or pass --to."
+ )
+
+ sender = current_app.config.get("MAIL_DEFAULT_SENDER")
+ if not sender:
+ raise click.ClickException("MAIL_DEFAULT_SENDER is not configured.")
+
+ message = Message(
+ subject="webmentions-ssg: test email",
+ sender=sender,
+ recipients=[recipient],
+ body=(
+ "This is a test email from webmentions-ssg.\n\n"
+ "If you received this message, the outgoing mail configuration works.\n"
+ ),
+ )
+
+ try:
+ MAIL.send(message)
+ except Exception as exc:
+ raise click.ClickException(f"Could not send test email: {exc}") from exc
+
+ click.echo(f"Test email sent to {recipient}.")