Skip to content

Commit

Permalink
implement common.mail.send()
Browse files Browse the repository at this point in the history
  • Loading branch information
samj committed Oct 14, 2024
1 parent c43f133 commit 09a4140
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
3 changes: 2 additions & 1 deletion __main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
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:
sys.path.append(str(base_dir))
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
Expand Down Expand Up @@ -48,6 +48,7 @@ def cleanup():

# Log connection details
logger.info(f"You can access pAI-OS at https://{host}:{port}.")
#asyncio.run(send("[email protected]", "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
Expand Down
45 changes: 45 additions & 0 deletions common/mail.py
Original file line number Diff line number Diff line change
@@ -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("[email protected]", "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}")
1 change: 1 addition & 0 deletions common/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# common requirements
cryptography
aiosmtplib

0 comments on commit 09a4140

Please sign in to comment.