Added multiple email support + json support, untested, should work/

This commit is contained in:
Uthman Fatih 2025-10-15 17:16:08 +01:00
parent 9b561d6983
commit 96cf658c3c
2 changed files with 22 additions and 17 deletions

33
main.py
View File

@ -12,43 +12,48 @@ import services.mail
from socket import gethostname from socket import gethostname
from dotenv import load_dotenv import json
from os import getenv from os.path import dirname, join, exists
from os.path import dirname, join
import typer import typer
# Load environment variables from .env file
dotenv_path = join(dirname(__file__), ".env")
load_dotenv(dotenv_path)
# FIXME: Error handling # FIXME: Error handling
# FIXME: Add json email list that if not added will use parameters
# FIXME: Handle no updates available
app = typer.Typer() app = typer.Typer()
@app.command() @app.command()
def serve(): def serve(receiver_email: str = None, check_interval: int = 24):
""" """
Checks for updates and emails hourly for security, daily for general as a daemon. Checks for updates at a fixed interval and sends email at that interval as a daemon. Doesn't email without updates.
Accepts check_interval as a parameter in hours (default 24)
""" """
@app.command() @app.command()
def now(receiver_email: str): def now(receiver_email: str = None):
if not receiver_email:
receiver_email = [email.strip() for email in receiver_email.split(",")]
""" """
Checks for apt upgrades and emails them then exits. Checks for apt upgrades and emails them then exits.
""" """
generate_email(receiver_email) generate_email(receiver_email)
def generate_email(receiver_email: str): def generate_email(receiver_email: list):
services.apt.require_root() services.apt.require_root()
if not services.apt.detect_apt(): if not services.apt.detect_apt():
print("Apt not found on this system.") print("Apt not found on this system.")
exit(1) exit(1)
updates = services.apt.check_updates() updates = services.apt.check_updates()
if exists(join(dirname(__file__), "users.json")):
with open(join(dirname(__file__), "users.json"), "r") as f:
users = json.load(f)
receiver_email = users
else:
if receiver_email is None:
print("No email address provided.")
exit(1)
# Get how many security updates are available # Get how many security updates are available
security_updates = 0 security_updates = 0
for package in updates: for package in updates:

View File

@ -31,20 +31,20 @@ IMAP_PORT = int(getenv("IMAP_PORT"))
IMAP_USERNAME = getenv("IMAP_USERNAME") IMAP_USERNAME = getenv("IMAP_USERNAME")
IMAP_PASSWORD = getenv("IMAP_PASSWORD") IMAP_PASSWORD = getenv("IMAP_PASSWORD")
def send_email(receiver_email, subject, body): def send_email(receiver_emails, subject, body):
sender_email = SMTP_USERNAME sender_email = SMTP_USERNAME
# Create the email # Create the email
message = MIMEText(body, "html") message = MIMEText(body, "html")
message["Subject"] = subject message["Subject"] = subject
message["From"] = sender_email message["From"] = sender_email
message["To"] = receiver_email message["To"] = ", ".join(receiver_emails)
# Connect using SSL # Connect using SSL
try: try:
with smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT) as server: with smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT) as server:
server.login(SMTP_USERNAME, SMTP_PASSWORD) server.login(SMTP_USERNAME, SMTP_PASSWORD)
server.sendmail(sender_email, receiver_email, message.as_string()) server.sendmail(sender_email, ", ".join(receiver_emails), message.as_string())
print("HTML email sent successfully!") print("HTML email sent successfully!")
except Exception as e: except Exception as e:
print(f"Failed to send email: {e}") print(f"Failed to send email: {e}")