This repository has been archived on 2024-11-02. You can view files and clone it, but cannot push or open issues or pull requests.
db-manager/src/mailer.py

60 lines
1.9 KiB
Python

# db-manager - Builds and manages an OwlBoard database instance - To be run on a
# cron schedule
# Copyright (C) 2023 Frederick Boniface
# This program is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with this
# program. If not, see
# https://git.fjla.uk/OwlBoard/db-manager/src/branch/main/LICENSE
import smtplib, ssl, os
import logger as log
log.out("mailer.py: Mailer module loaded", "DBUG")
def submitLogs():
text :str = fetchLogs()
sendMail(text)
def fetchLogs():
with open("dbman-log", "r") as tmpfile:
return tmpfile.read()
def deleteLogs():
if os.path.exists("dbman-log"):
os.remove("dbman-log")
print("Tidied log file")
else:
print("No logfile to tidy")
def sendMail(messageBody :str):
smtpHost = os.getenv("OWL_EML_HOST")
smtpPort = os.getenv("OWL_EML_PORT")
smtpUser = os.getenv("OWL_EML_USER")
smtpPass = os.getenv("OWL_EML_PASS")
smtpFrom = os.getenv("OWL_EML_FROM")
context = ssl.create_default_context()
message = f"""Subject: OwlBoard-dbman-logs
{messageBody}"""
try:
server = smtplib.SMTP(smtpHost,smtpPort)
server.ehlo()
server.starttls(context=context) # Secure the connection
server.ehlo()
server.login(smtpUser, smtpPass)
server.sendmail(smtpFrom, "server-notification-receipt@fjla.uk", message)
except Exception as e:
# Print any error messages to stdout
print(e)
finally:
server.quit()
deleteLogs()