diff --git a/src/BreCalClient/App.config b/src/BreCalClient/App.config index 335a433..d6fc131 100644 --- a/src/BreCalClient/App.config +++ b/src/BreCalClient/App.config @@ -38,7 +38,7 @@ https://www.textbausteine.net/ - https://brecaldevel.bsmd-emswe.eu + http://127.0.0.1:5000 diff --git a/src/BreCalClient/Properties/Settings.Designer.cs b/src/BreCalClient/Properties/Settings.Designer.cs index fd85a6b..229f8e9 100644 --- a/src/BreCalClient/Properties/Settings.Designer.cs +++ b/src/BreCalClient/Properties/Settings.Designer.cs @@ -9,20 +9,20 @@ //------------------------------------------------------------------------------ namespace BreCalClient.Properties { - - + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.10.0.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { - + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); - + public static Settings Default { get { return defaultInstance; } } - + [global::System.Configuration.ApplicationScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("#1D751F")] @@ -31,7 +31,7 @@ namespace BreCalClient.Properties { return ((string)(this["BG_COLOR"])); } } - + [global::System.Configuration.ApplicationScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("!!Bremen calling Testversion!!")] @@ -40,7 +40,7 @@ namespace BreCalClient.Properties { return ((string)(this["APP_TITLE"])); } } - + [global::System.Configuration.ApplicationScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("https://www.textbausteine.net/")] @@ -49,7 +49,7 @@ namespace BreCalClient.Properties { return ((string)(this["LOGO_IMAGE_URL"])); } } - + [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("")] @@ -61,16 +61,16 @@ namespace BreCalClient.Properties { this["FilterCriteria"] = value; } } - + [global::System.Configuration.ApplicationScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("https://brecaldevel.bsmd-emswe.eu")] + [global::System.Configuration.DefaultSettingValueAttribute("http://127.0.0.1:5000")] public string API_URL { get { return ((string)(this["API_URL"])); } } - + [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("800")] @@ -82,7 +82,7 @@ namespace BreCalClient.Properties { this["Width"] = value; } } - + [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("450")] @@ -94,7 +94,7 @@ namespace BreCalClient.Properties { this["Height"] = value; } } - + [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("0")] @@ -106,7 +106,7 @@ namespace BreCalClient.Properties { this["Left"] = value; } } - + [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("0")] @@ -118,7 +118,7 @@ namespace BreCalClient.Properties { this["Top"] = value; } } - + [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("0")] @@ -130,7 +130,7 @@ namespace BreCalClient.Properties { this["W1Left"] = value; } } - + [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("0")] @@ -142,7 +142,7 @@ namespace BreCalClient.Properties { this["W1Top"] = value; } } - + [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("0")] @@ -154,7 +154,7 @@ namespace BreCalClient.Properties { this["W2Left"] = value; } } - + [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("0")] @@ -166,7 +166,7 @@ namespace BreCalClient.Properties { this["W2Top"] = value; } } - + [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("0")] @@ -178,7 +178,7 @@ namespace BreCalClient.Properties { this["W3Left"] = value; } } - + [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("0")] @@ -190,7 +190,7 @@ namespace BreCalClient.Properties { this["W3Top"] = value; } } - + [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("0")] @@ -202,7 +202,7 @@ namespace BreCalClient.Properties { this["W4Left"] = value; } } - + [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("0")] @@ -214,7 +214,7 @@ namespace BreCalClient.Properties { this["W4Top"] = value; } } - + [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("")] diff --git a/src/BreCalClient/Properties/Settings.settings b/src/BreCalClient/Properties/Settings.settings index b10fd5b..7fd6d5d 100644 --- a/src/BreCalClient/Properties/Settings.settings +++ b/src/BreCalClient/Properties/Settings.settings @@ -15,7 +15,7 @@ - https://brecaldevel.bsmd-emswe.eu + http://127.0.0.1:5000 800 diff --git a/src/server/BreCal/local_db.py b/src/server/BreCal/local_db.py index 330bc13..08846e4 100644 --- a/src/server/BreCal/local_db.py +++ b/src/server/BreCal/local_db.py @@ -4,6 +4,7 @@ import logging import json import os import sys +import schemas.defs as defs config_path = None @@ -13,6 +14,7 @@ def initPool(instancePath, connection_filename="connection_data_devel.json"): if(config_path == None): config_path = os.path.join(instancePath,f'../../../secure/{connection_filename}') #connection_data_devel.json'); + config_path = "E:/temp/connection_data.json" print (config_path) if not os.path.exists(config_path): @@ -22,6 +24,7 @@ def initPool(instancePath, connection_filename="connection_data_devel.json"): f = open(config_path); connection_data = json.load(f) + f.close() conn_from_pool = mysql.connector.connect(**connection_data) @@ -29,6 +32,16 @@ def initPool(instancePath, connection_filename="connection_data_devel.json"): data = commands.query("SELECT id from `user`") print("DB connection successful") conn_from_pool.close() + + credentials_file = "email_credentials_devel.json" + credentials_path = os.path.join(instancePath,f'../../../secure/{credentials_file}') + if not os.path.exists(credentials_path): + print ('cannot find ' + os.path.abspath(credentials_path)) + sys.exit(1) + f = open(credentials_path); + defs.email_credentials = json.load(f) + f.close() + except mysql.connector.PoolError as e: logging.error(f"Failed to create connection pool: {e}") print(e) diff --git a/src/server/BreCal/schemas/defs.py b/src/server/BreCal/schemas/defs.py index 90a56b5..8e8ccea 100644 --- a/src/server/BreCal/schemas/defs.py +++ b/src/server/BreCal/schemas/defs.py @@ -2,4 +2,7 @@ # Constants for the notification system NOTIFICATION_COOLDOWN = 600 # 10 minutes until a notification gets real -NOTIFICATION_MAX_AGE_DAYS = 3 # 3 days until a notification gets deleted \ No newline at end of file +NOTIFICATION_MAX_AGE_DAYS = 3 # 3 days until a notification gets deleted + +# Placeholder for the email credentials filled by startup logic +email_credentials = dict() \ No newline at end of file diff --git a/src/server/BreCal/services/schedule_routines.py b/src/server/BreCal/services/schedule_routines.py index ab58c4d..401e9f4 100644 --- a/src/server/BreCal/services/schedule_routines.py +++ b/src/server/BreCal/services/schedule_routines.py @@ -1,6 +1,9 @@ import logging import pydapper -from BreCal.schemas import model +import smtplib +from email.message import EmailMessage + +from BreCal.schemas import model, defs from BreCal.local_db import getPoolConnection from BreCal.database.update_database import evaluate_shipcall_state from BreCal.database.sql_queries import create_sql_query_shipcall_get @@ -67,6 +70,35 @@ def UpdateNotifications(): except Exception as ex: logging.error(ex) +def SendEmails(email_dict): + """ + This function sends emails to all users in the emaildict + """ + try: + conn = smtplib.SMTP_SSL(defs.email_credentials["server"], defs.email_credentials["port"]) + conn.set_debuglevel(1) + conn.ehlo() + conn.starttls() + conn.ehlo() + conn.login(defs.email_credentials["sender"], defs.email_credentials["password_send"]) + + for user, message in email_dict.items(): + msg = EmailMessage() + msg["Subject"] = '[Bremen calling] Notification' + msg["From"] = defs.email_credentials["sender"] + msg["To"] = user.user_email + + # TODO: pretty-print and format message according to template + msg.set_content(message) + + conn.sendmail(user.user_email, user.user_email, msg.as_string()) + + except Exception as ex: + logging.error(ex) + finally: + conn.quit() + + def SendNotifications(): # perhaps this will be moved somewhere else later try: @@ -127,11 +159,10 @@ def SendNotifications(): # mark as sent commands.execute("UPDATE notification SET level = 2 WHERE id = ?id?", param={"id":notification.id}) - # send emails + # send emails (if any) if len(email_dict) > 0: - email_handler = EmailHandler(mail_server="smtp.gmail.com", mail_port=465, mail_address="") - for user, message in email_dict.items(): - email_handler.send_email(user.user_email, "BreCal Notification", message) + SendEmails(email_dict) + except Exception as ex: logging.error(ex) diff --git a/src/server/tests/brecal_utils/email_test.py b/src/server/tests/brecal_utils/email_test.py new file mode 100644 index 0000000..00fc9fd --- /dev/null +++ b/src/server/tests/brecal_utils/email_test.py @@ -0,0 +1,49 @@ +import os +import smtplib +import json +import sys + +from email.message import EmailMessage + +path_to_email_credentials = os.path.join(os.path.dirname(__file__), "email_credentials.json") + +def send_email_example(credentials, recipient): + """ + sends an email to the given email address + :param email: the email address to send the email to + :param subject: the subject of the email + :param message: the message of the email + :return: None + """ + # create the email message + msg = EmailMessage() + + + msg["Subject"] = '[Bremen calling] Test Email' + msg["From"] = credentials["sender"] + + conn = smtplib.SMTP(credentials["server"], credentials["port"]) + conn.set_debuglevel(1) + conn.ehlo() + conn.starttls() + conn.ehlo() + conn.login(credentials["sender"], credentials["password_send"]) + try: + conn.sendmail(credentials["sender"], recipient, msg.as_string()) + except smtplib.SMTPException as ex: + print(f"Error: {ex}") + finally: + conn.quit() + + +config_path = "E:/temp/email_credentials.json" +print (config_path) + +if not os.path.exists(config_path): + print ('cannot find ' + os.path.abspath(config_path)) + sys.exit(1) + +f = open(config_path); +credentials = json.load(f) + +send_email_example(credentials, "bald@puls200.de") \ No newline at end of file