Added multiple email support + json support, untested, should work/
This commit is contained in:
parent
9b561d6983
commit
96cf658c3c
33
main.py
33
main.py
@ -12,43 +12,48 @@ import services.mail
|
||||
|
||||
from socket import gethostname
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from os import getenv
|
||||
from os.path import dirname, join
|
||||
import json
|
||||
from os.path import dirname, join, exists
|
||||
|
||||
import typer
|
||||
|
||||
# Load environment variables from .env file
|
||||
dotenv_path = join(dirname(__file__), ".env")
|
||||
load_dotenv(dotenv_path)
|
||||
|
||||
# FIXME: Error handling
|
||||
# FIXME: Add json email list that if not added will use parameters
|
||||
# FIXME: Handle no updates available
|
||||
|
||||
app = typer.Typer()
|
||||
|
||||
@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()
|
||||
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.
|
||||
"""
|
||||
generate_email(receiver_email)
|
||||
|
||||
def generate_email(receiver_email: str):
|
||||
def generate_email(receiver_email: list):
|
||||
services.apt.require_root()
|
||||
if not services.apt.detect_apt():
|
||||
print("Apt not found on this system.")
|
||||
exit(1)
|
||||
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
|
||||
security_updates = 0
|
||||
for package in updates:
|
||||
|
||||
@ -31,20 +31,20 @@ IMAP_PORT = int(getenv("IMAP_PORT"))
|
||||
IMAP_USERNAME = getenv("IMAP_USERNAME")
|
||||
IMAP_PASSWORD = getenv("IMAP_PASSWORD")
|
||||
|
||||
def send_email(receiver_email, subject, body):
|
||||
def send_email(receiver_emails, subject, body):
|
||||
sender_email = SMTP_USERNAME
|
||||
|
||||
# Create the email
|
||||
message = MIMEText(body, "html")
|
||||
message["Subject"] = subject
|
||||
message["From"] = sender_email
|
||||
message["To"] = receiver_email
|
||||
message["To"] = ", ".join(receiver_emails)
|
||||
|
||||
# Connect using SSL
|
||||
try:
|
||||
with smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT) as server:
|
||||
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!")
|
||||
except Exception as e:
|
||||
print(f"Failed to send email: {e}")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user