add logger to mail
This commit is contained in:
@@ -1,186 +1,197 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from omegaconf import OmegaConf
|
from omegaconf import OmegaConf
|
||||||
from PyQt6 import QtCore, QtGui, QtWidgets
|
from PyQt6 import QtCore, QtGui, QtWidgets
|
||||||
|
|
||||||
from .dialog_sources.Ui_mail_preview import Ui_eMailPreview as Ui_Dialog
|
from src.logic import log
|
||||||
|
|
||||||
config = OmegaConf.load("config.yaml")
|
from .dialog_sources.Ui_mail_preview import Ui_eMailPreview as Ui_Dialog
|
||||||
|
|
||||||
|
config = OmegaConf.load("config.yaml")
|
||||||
class Mail_Dialog(QtWidgets.QDialog, Ui_Dialog):
|
from src.logic.log import MyLogger
|
||||||
def __init__(
|
|
||||||
self,
|
logger = MyLogger("Mail")
|
||||||
app_id,
|
|
||||||
app_name,
|
|
||||||
app_subject,
|
class Mail_Dialog(QtWidgets.QDialog, Ui_Dialog):
|
||||||
prof_name,
|
def __init__(
|
||||||
prof_mail,
|
self,
|
||||||
parent=None,
|
app_id,
|
||||||
default_mail="Information zum Semesterapparat",
|
app_name,
|
||||||
):
|
app_subject,
|
||||||
super().__init__(parent)
|
prof_name,
|
||||||
self.setupUi(
|
prof_mail,
|
||||||
self,
|
parent=None,
|
||||||
# app_id,
|
default_mail="Information zum Semesterapparat",
|
||||||
# app_name,
|
):
|
||||||
# app_subject,
|
super().__init__(parent)
|
||||||
# prof_name,
|
self.setupUi(
|
||||||
)
|
self,
|
||||||
self.appid = app_id
|
# app_id,
|
||||||
self.appname = app_name
|
# app_name,
|
||||||
self.subject = app_subject
|
# app_subject,
|
||||||
self.profname = prof_name
|
# prof_name,
|
||||||
self.mail_data = ""
|
)
|
||||||
self.prof_mail = prof_mail
|
self.appid = app_id
|
||||||
self.comboBox.currentIndexChanged.connect(self.set_mail)
|
self.appname = app_name
|
||||||
self.prof_name.setText(prof_name)
|
self.subject = app_subject
|
||||||
self.mail_name.setText(self.prof_mail)
|
self.profname = prof_name
|
||||||
self.load_mail_templates()
|
self.mail_data = ""
|
||||||
# if none of the radio buttons is checked, disable the accept button of the dialog
|
self.prof_mail = prof_mail
|
||||||
self.btn_okay.setEnabled(False)
|
self.comboBox.currentIndexChanged.connect(self.set_mail)
|
||||||
|
self.prof_name.setText(prof_name)
|
||||||
if default_mail is not None:
|
self.mail_name.setText(self.prof_mail)
|
||||||
# get the nearest match to the default mail
|
self.load_mail_templates()
|
||||||
for i in range(self.comboBox.count()):
|
# if none of the radio buttons is checked, disable the accept button of the dialog
|
||||||
if default_mail in self.comboBox.itemText(i):
|
self.btn_okay.setEnabled(False)
|
||||||
default_mail = self.comboBox.itemText(i)
|
|
||||||
break
|
if default_mail is not None:
|
||||||
self.comboBox.setCurrentText(default_mail)
|
# get the nearest match to the default mail
|
||||||
|
for i in range(self.comboBox.count()):
|
||||||
self.gender_female.clicked.connect(self.set_mail)
|
if default_mail in self.comboBox.itemText(i):
|
||||||
self.gender_male.clicked.connect(self.set_mail)
|
default_mail = self.comboBox.itemText(i)
|
||||||
self.gender_non.clicked.connect(self.set_mail)
|
break
|
||||||
self.btn_okay.clicked.connect(self.createAndSendMail)
|
self.comboBox.setCurrentText(default_mail)
|
||||||
# self.send_button.clicked.connect(self.save_mail)
|
|
||||||
|
self.gender_female.clicked.connect(self.set_mail)
|
||||||
# def set_data(self, data: dict):
|
self.gender_male.clicked.connect(self.set_mail)
|
||||||
# print(data)
|
self.gender_non.clicked.connect(self.set_mail)
|
||||||
# self.prof_name.setText(data["prof_name"])
|
self.btn_okay.clicked.connect(self.createAndSendMail)
|
||||||
# self.mail_name.setText(data["mail_name"])
|
# self.send_button.clicked.connect(self.save_mail)
|
||||||
# # assign data["general"] to self.data
|
|
||||||
# self.data = data["general"]
|
# def set_data(self, data: dict):
|
||||||
|
# print(data)
|
||||||
def load_mail_templates(self):
|
# self.prof_name.setText(data["prof_name"])
|
||||||
print("loading mail templates")
|
# self.mail_name.setText(data["mail_name"])
|
||||||
mail_templates = os.listdir("mail_vorlagen")
|
# # assign data["general"] to self.data
|
||||||
for template in mail_templates:
|
# self.data = data["general"]
|
||||||
self.comboBox.addItem(template)
|
|
||||||
|
def load_mail_templates(self):
|
||||||
def get_greeting(self):
|
print("loading mail templates")
|
||||||
prof = self.profname
|
|
||||||
if self.gender_male.isChecked():
|
mail_templates = os.listdir("mail_vorlagen")
|
||||||
self.btn_okay.setEnabled(True)
|
logger.log_info(f"Mail templates: {mail_templates}")
|
||||||
return f"Sehr geehrter Herr {prof.split(' ')[-1]},"
|
for template in mail_templates:
|
||||||
elif self.gender_female.isChecked():
|
self.comboBox.addItem(template)
|
||||||
self.btn_okay.setEnabled(True)
|
|
||||||
return f"Sehr geehrte Frau {prof.split(' ')[-1]},"
|
def get_greeting(self):
|
||||||
elif self.gender_non.isChecked():
|
prof = self.profname
|
||||||
self.btn_okay.setEnabled(True)
|
if self.gender_male.isChecked():
|
||||||
return f"Guten Tag {prof},"
|
self.btn_okay.setEnabled(True)
|
||||||
|
return f"Sehr geehrter Herr {prof.split(' ')[-1]},"
|
||||||
def set_mail(self):
|
elif self.gender_female.isChecked():
|
||||||
email_template = self.comboBox.currentText()
|
self.btn_okay.setEnabled(True)
|
||||||
if email_template == "":
|
return f"Sehr geehrte Frau {prof.split(' ')[-1]},"
|
||||||
return
|
elif self.gender_non.isChecked():
|
||||||
with open(f"mail_vorlagen/{email_template}", "r", encoding="utf-8") as f:
|
self.btn_okay.setEnabled(True)
|
||||||
mail_template = f.read()
|
return f"Guten Tag {prof},"
|
||||||
email_header = email_template.split(".eml")[0]
|
|
||||||
if "{AppNr}" in email_template:
|
def set_mail(self):
|
||||||
email_header = email_template.split(".eml")[0]
|
email_template = self.comboBox.currentText()
|
||||||
email_header = email_header.format(AppNr=self.appid, AppName=self.appname)
|
if email_template == "":
|
||||||
self.mail_header.setText(email_header)
|
return
|
||||||
self.mail_data = mail_template.split("<html>")[0]
|
with open(f"mail_vorlagen/{email_template}", "r", encoding="utf-8") as f:
|
||||||
mail_html = mail_template.split("<html>")[1]
|
mail_template = f.read()
|
||||||
mail_html = "<html>" + mail_html
|
email_header = email_template.split(".eml")[0]
|
||||||
Appname = self.appname
|
if "{AppNr}" in email_template:
|
||||||
mail_html = mail_html.format(
|
email_header = email_template.split(".eml")[0]
|
||||||
Profname=self.prof_name.text().split(" ")[-1],
|
email_header = email_header.format(AppNr=self.appid, AppName=self.appname)
|
||||||
Appname=Appname,
|
self.mail_header.setText(email_header)
|
||||||
AppNr=self.appid,
|
self.mail_data = mail_template.split("<html>")[0]
|
||||||
AppSubject=self.subject,
|
mail_html = mail_template.split("<html>")[1]
|
||||||
greeting=self.get_greeting(),
|
mail_html = "<html>" + mail_html
|
||||||
)
|
Appname = self.appname
|
||||||
|
mail_html = mail_html.format(
|
||||||
self.mail_body.setHtml(mail_html)
|
Profname=self.prof_name.text().split(" ")[-1],
|
||||||
|
Appname=Appname,
|
||||||
def createAndSendMail(self):
|
AppNr=self.appid,
|
||||||
import smtplib
|
AppSubject=self.subject,
|
||||||
import ssl
|
greeting=self.get_greeting(),
|
||||||
from email.mime.multipart import MIMEMultipart
|
)
|
||||||
from email.mime.text import MIMEText
|
|
||||||
tolist = [self.prof_mail, "semesterapparate@ph-freiburg.de"]
|
self.mail_body.setHtml(mail_html)
|
||||||
self.btn_okay.setText("Mail wird gesendet")
|
logger.log_info(f"Mail template set to {email_template}")
|
||||||
smtp_server = config["mail"]["smtp_server"]
|
|
||||||
port: int = config["mail"]["port"]
|
def createAndSendMail(self):
|
||||||
sender_email = config["mail"]["sender"]
|
logger.log_info("Sending mail")
|
||||||
password = config["mail"]["password"]
|
import smtplib
|
||||||
message = MIMEMultipart()
|
import ssl
|
||||||
message["From"] = sender_email
|
from email.mime.multipart import MIMEMultipart
|
||||||
message["To"] = self.prof_mail
|
from email.mime.text import MIMEText
|
||||||
message["Subject"] = self.mail_header.text()
|
|
||||||
#include a Fcc to the senders sent folder
|
tolist = [self.prof_mail, "semesterapparate@ph-freiburg.de"]
|
||||||
message["cc"] = "semesterapparate@ph-freiburg.de"
|
self.btn_okay.setText("Mail wird gesendet")
|
||||||
|
smtp_server = config["mail"]["smtp_server"]
|
||||||
mail_body = self.mail_body.toHtml()
|
port: int = config["mail"]["port"]
|
||||||
message.attach(MIMEText(mail_body, "html"))
|
sender_email = config["mail"]["sender"]
|
||||||
mail = message.as_string()
|
password = config["mail"]["password"]
|
||||||
|
message = MIMEMultipart()
|
||||||
server = smtplib.SMTP_SSL(smtp_server, port)
|
message["From"] = sender_email
|
||||||
# server.starttls()
|
message["To"] = self.prof_mail
|
||||||
# server.auth(mechanism="PLAIN")
|
message["Subject"] = self.mail_header.text()
|
||||||
if config["mail"]["use_user_name"] == 1:
|
# include a Fcc to the senders sent folder
|
||||||
print(config["mail"]["user_name"])
|
message["cc"] = "semesterapparate@ph-freiburg.de"
|
||||||
server.login(config["mail"]["user_name"], password)
|
|
||||||
else:
|
mail_body = self.mail_body.toHtml()
|
||||||
server.login(sender_email, password)
|
message.attach(MIMEText(mail_body, "html"))
|
||||||
server.sendmail(sender_email, tolist, mail)
|
mail = message.as_string()
|
||||||
print("Mail sent")
|
|
||||||
# end active process
|
server = smtplib.SMTP_SSL(smtp_server, port)
|
||||||
server.quit()
|
# server.starttls()
|
||||||
#close the dialog
|
# server.auth(mechanism="PLAIN")
|
||||||
|
if config["mail"]["use_user_name"] == 1:
|
||||||
self.accept()
|
print(config["mail"]["user_name"])
|
||||||
# # create a temporary file
|
server.login(config["mail"]["user_name"], password)
|
||||||
# mail_header = self.mail_header.text()
|
else:
|
||||||
# mail_body = self.mail_body.toHtml()
|
server.login(sender_email, password)
|
||||||
# mail = self.mail_data + mail_body
|
server.sendmail(sender_email, tolist, mail)
|
||||||
# mail = mail.replace("Subject:", f"Subject: {mail_header}")
|
print("Mail sent")
|
||||||
# directory = config["database"]["tempdir"]
|
# end active process
|
||||||
# directory = directory.replace("~", str(os.path.expanduser("~")))
|
server.quit()
|
||||||
# with tempfile.NamedTemporaryFile(
|
logger.log_info("Mail sent, closing connection to server and dialog")
|
||||||
# mode="w", delete=False, suffix=".eml", encoding="utf-8", dir=directory
|
# close the dialog
|
||||||
# ) as f:
|
|
||||||
# f.write(mail)
|
self.accept()
|
||||||
# self.mail_path = f.name
|
# # create a temporary file
|
||||||
# print(self.mail_path)
|
# mail_header = self.mail_header.text()
|
||||||
# # open the file using thunderbird
|
# mail_body = self.mail_body.toHtml()
|
||||||
# subprocess.Popen([f"{self.mail_path}"])
|
# mail = self.mail_data + mail_body
|
||||||
# # delete the file
|
# mail = mail.replace("Subject:", f"Subject: {mail_header}")
|
||||||
# # os.remove(self.mail_path)
|
# directory = config["database"]["tempdir"]
|
||||||
|
# directory = directory.replace("~", str(os.path.expanduser("~")))
|
||||||
|
# with tempfile.NamedTemporaryFile(
|
||||||
def launch_gui(app_id="", app_name="", app_subject="", prof_name="", prof_mail=""):
|
# mode="w", delete=False, suffix=".eml", encoding="utf-8", dir=directory
|
||||||
app = QtWidgets.QApplication(sys.argv)
|
# ) as f:
|
||||||
dialog = Mail_Dialog(
|
# f.write(mail)
|
||||||
app_id=app_id,
|
# self.mail_path = f.name
|
||||||
app_name=app_name,
|
# print(self.mail_path)
|
||||||
app_subject=app_subject,
|
# # open the file using thunderbird
|
||||||
prof_name=prof_name,
|
# subprocess.Popen([f"{self.mail_path}"])
|
||||||
prof_mail=prof_mail,
|
# # delete the file
|
||||||
default_mail="Information bezüglich der Auflösung des Semesterapparates",
|
# # os.remove(self.mail_path)
|
||||||
)
|
|
||||||
dialog.show()
|
|
||||||
sys.exit(app.exec())
|
def launch_gui(app_id="", app_name="", app_subject="", prof_name="", prof_mail=""):
|
||||||
|
app = QtWidgets.QApplication(sys.argv)
|
||||||
|
dialog = Mail_Dialog(
|
||||||
if __name__ == "__main__":
|
app_id=app_id,
|
||||||
import sys
|
app_name=app_name,
|
||||||
|
app_subject=app_subject,
|
||||||
app = QtWidgets.QApplication(sys.argv)
|
prof_name=prof_name,
|
||||||
Dialog = QtWidgets.QDialog()
|
prof_mail=prof_mail,
|
||||||
ui = Mail_Dialog()
|
default_mail="Information bezüglich der Auflösung des Semesterapparates",
|
||||||
ui.setupUi(Dialog)
|
)
|
||||||
Dialog.show()
|
dialog.show()
|
||||||
sys.exit(app.exec())
|
sys.exit(app.exec())
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import sys
|
||||||
|
|
||||||
|
app = QtWidgets.QApplication(sys.argv)
|
||||||
|
Dialog = QtWidgets.QDialog()
|
||||||
|
ui = Mail_Dialog()
|
||||||
|
ui.setupUi(Dialog)
|
||||||
|
Dialog.show()
|
||||||
|
sys.exit(app.exec())
|
||||||
|
|||||||
Reference in New Issue
Block a user