www_site/infra/scripts/kaotings-alert.py

29 lines
978 B
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
import os
import smtplib
import ssl
import sys
from email.message import EmailMessage
def main() -> int:
subject = sys.argv[1] if len(sys.argv) > 1 else "Kaotings production alert"
body = sys.stdin.read() or "No details provided."
recipients = [item.strip() for item in os.environ.get("ALERT_RECIPIENTS", "").split(",") if item.strip()]
if not recipients:
raise SystemExit("ALERT_RECIPIENTS is required")
message = EmailMessage()
message["From"] = os.environ["SMTP_FROM"]
message["To"] = ", ".join(recipients)
message["Subject"] = subject
message.set_content(body)
context = ssl.create_default_context()
with smtplib.SMTP_SSL(os.environ["SMTP_HOST"], int(os.environ.get("SMTP_PORT", "465")), context=context, timeout=20) as smtp:
smtp.login(os.environ["SMTP_USER"], os.environ["SMTP_PASSWORD"])
smtp.send_message(message)
return 0
if __name__ == "__main__":
sys.exit(main())