From 09a4140286ca4302a3b2292724775ab23e20a356 Mon Sep 17 00:00:00 2001 From: Sam Johnston Date: Mon, 14 Oct 2024 13:02:48 -0700 Subject: [PATCH] implement common.mail.send() --- __main__.py | 3 ++- common/mail.py | 45 +++++++++++++++++++++++++++++++++++++++++ common/requirements.txt | 1 + 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 common/mail.py diff --git a/__main__.py b/__main__.py index 72b1aea2..c5c13070 100644 --- a/__main__.py +++ b/__main__.py @@ -3,7 +3,6 @@ import sys import signal from pathlib import Path - # Ensure the parent directory is in sys.path so relative imports work. base_dir = Path(__file__).parent if base_dir not in sys.path: @@ -11,6 +10,7 @@ from common.paths import backend_dir, venv_dir, cert_dir from common.config import logging_config from backend.utils import get_env_key +from common.mail import send # check environment from backend.env import check_env @@ -48,6 +48,7 @@ def cleanup(): # Log connection details logger.info(f"You can access pAI-OS at https://{host}:{port}.") + #asyncio.run(send("samj@samj.net", "pAI-OS started up", f"You can access pAI-OS at https://{host}:{port}.")) logger.info("Bypass certificate warnings if using self-signed certificates.") # Run the app diff --git a/common/mail.py b/common/mail.py new file mode 100644 index 00000000..5f2b931d --- /dev/null +++ b/common/mail.py @@ -0,0 +1,45 @@ +import aiosmtplib +import secrets +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText +from .utils import get_env_key # Assuming get_env_key is in utils module + +async def send(to, subject, body_text, body_html = None): + # sends mail via SMTP in text and/or html format + # asyncio.run(send("samj@samj.net", "pAI-OS started up", f"You can access pAI-OS at https://{host}:{port}.")) + + # Retrieve SMTP server details from environment variables + smtp_host = get_env_key('PAIOS_SMTP_HOST', 'localhost') + smtp_port = get_env_key('PAIOS_SMTP_PORT', '587') # Default SMTP port for STARTTLS + smtp_from = get_env_key('PAIOS_SMTP_FROM', 'paios@localhost') + smtp_user = get_env_key('PAIOS_SMTP_USER', 'paios@localhost') + smtp_pass = get_env_key('PAIOS_SMTP_PASS', secrets.token_urlsafe(32)) + + # Create a MIME message + msg = MIMEMultipart('alternative') + msg['Subject'] = subject + msg['From'] = smtp_from + msg['To'] = to + + # Ensure body_html is not None + if body_html is None: + body_html = body_text # Fallback to plain text if HTML is not provided + + # Attach both plain text and HTML parts + part1 = MIMEText(body_text, 'plain') + part2 = MIMEText(body_html, 'html') + msg.attach(part1) + msg.attach(part2) + + # Connect to the SMTP server and send the email + try: + await aiosmtplib.send( + msg, + hostname=smtp_host, + port=smtp_port, + start_tls=True, + username=smtp_user, + password=smtp_pass, + ) + except Exception as e: + print(f"Failed to send email: {e}") diff --git a/common/requirements.txt b/common/requirements.txt index 0db6e271..1eadf51d 100644 --- a/common/requirements.txt +++ b/common/requirements.txt @@ -1,2 +1,3 @@ # common requirements cryptography +aiosmtplib