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