From 34a7c327b1c46e12ee0776f32df2e369ad05346b Mon Sep 17 00:00:00 2001 From: ChuckNorrison <2964146+ChuckNorrison@users.noreply.github.com> Date: Tue, 8 Oct 2024 22:14:46 +0200 Subject: [PATCH] Add configurable email server Based on https://github.com/Hydrosys4/Master/pull/17 Add default values for server and port. --- emaildbmod.py | 46 ++++++++++++++++++++++---------- emailmod.py | 2 +- start.py | 11 ++++++-- templates/systemmailsetting.html | 14 +++++++++- 4 files changed, 55 insertions(+), 18 deletions(-) diff --git a/emaildbmod.py b/emaildbmod.py index f3e3b70..273d5fe 100755 --- a/emaildbmod.py +++ b/emaildbmod.py @@ -2,7 +2,7 @@ """ fertilizer UI setting storage utilities """ -from __future__ import print_function +from __future__ import print_function import logging import os @@ -26,13 +26,11 @@ # if file does not exist create file data=[] if not filestoragemod.readfiledata(DATAFILENAME,data): #read setting file - filedata=[{'name':'email', 'address':'','password':'' }] - filestoragemod.savefiledata(DATAFILENAME,filedata) + restoredefault() def savedata(filedata): filestoragemod.savefiledata(DATAFILENAME,filedata) - def getelementlist(): recordkey=hardwaremod.HW_FUNC_USEDFOR recordvalue="mailcontrol" @@ -41,8 +39,6 @@ def getelementlist(): #print "elementlist= ",datalist return datalist - - def getaddress(): recordkey="name" recordvalue="email" @@ -57,6 +53,25 @@ def getpassword(): dataitem=filestoragemod.searchdata(DATAFILENAME,recordkey,recordvalue,keytosearch) return dataitem +def getserver(): + recordkey="name" + recordvalue="email" + keytosearch="server" + dataitem=filestoragemod.searchdata(DATAFILENAME,recordkey,recordvalue,keytosearch) + # set default + if dataitem == "": + dataitem = "smtp.gmail.com" + return dataitem + +def getport(): + recordkey="name" + recordvalue="email" + keytosearch="port" + dataitem=filestoragemod.searchdata(DATAFILENAME,recordkey,recordvalue,keytosearch) + # set default + if dataitem == "": + dataitem = "587" + return dataitem def changesavesetting(FTparameter,FTvalue): searchfield="name" @@ -68,7 +83,7 @@ def changesavesetting(FTparameter,FTvalue): def restoredefault(): filestoragemod.deletefile(DATAFILENAME) - filedata=[{'name':'email', 'address':'','password':'' }] + filedata=[{'name':'email', 'address':'','password':'', 'server': 'smtp.gmail.com', 'port': '587'}] filestoragemod.savefiledata(DATAFILENAME,filedata) def get_path(): @@ -92,10 +107,13 @@ def get_path(): # comment address="hello@mail.com" password="haha" - changesavesetting("address",address) - changesavesetting("password",password) - print(getaddress()) - print(getpassword()) - - - + server="smtp.gmail.com" + port="587" + #changesavesetting("address",address) + #changesavesetting("password",password) + #changesavesetting("server",server) + #changesavesetting("port",port) + #print(getaddress()) + #print(getpassword()) + print(getserver()) + print(getport()) diff --git a/emailmod.py b/emailmod.py index 8c001b3..49286da 100755 --- a/emailmod.py +++ b/emailmod.py @@ -43,7 +43,7 @@ def send_email(user, pwd, recipient, subject, body): message = """\From: %s\nTo: %s\nSubject: %s\n\n%s """ % (FROM, ", ".join(TO), SUBJECT, TEXT) try: - server = smtplib.SMTP("smtp.gmail.com", 587) + server = smtplib.SMTP(emaildbmod.getserver(), emaildbmod.getport()) server.ehlo() server.starttls() server.login(gmail_user, gmail_pwd) diff --git a/start.py b/start.py index be7718d..68dbba9 100755 --- a/start.py +++ b/start.py @@ -1329,9 +1329,13 @@ def systemmailsetting(): print("saving email credentials") address=request.form['address'] password=request.form['password'] + server=request.form['server'] + port=request.form['port'] isok1=emaildbmod.changesavesetting('address',address) isok2=emaildbmod.changesavesetting('password',password) - if isok1 and isok2: + isok3=emaildbmod.changesavesetting('server',server) + isok4=emaildbmod.changesavesetting('port',port) + if isok1 and isok2 and isok3 and isok4: flash('Email credentials Saved') return redirect(url_for('show_Calibration')) elif reqtype=="cancel": @@ -1339,7 +1343,10 @@ def systemmailsetting(): password=emaildbmod.getpassword() address=emaildbmod.getaddress() - return render_template('systemmailsetting.html', address=address, password=password) + server=emaildbmod.getserver() + port=emaildbmod.getport() + + return render_template('systemmailsetting.html', address=address, password=password, server=server, port=port) @application.route('/HC12setting/', methods=['GET', 'POST']) def HC12setting(): diff --git a/templates/systemmailsetting.html b/templates/systemmailsetting.html index 8015853..8339b29 100644 --- a/templates/systemmailsetting.html +++ b/templates/systemmailsetting.html @@ -23,7 +23,19 @@