Skip to content

Commit

Permalink
Add configurable email server
Browse files Browse the repository at this point in the history
Based on Hydrosys4#17

Add default values for server and port.
  • Loading branch information
ChuckNorrison committed Oct 8, 2024
1 parent 0c11b0c commit 34a7c32
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 18 deletions.
46 changes: 32 additions & 14 deletions emaildbmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""
fertilizer UI setting storage utilities
"""
from __future__ import print_function
from __future__ import print_function

import logging
import os
Expand All @@ -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"
Expand All @@ -41,8 +39,6 @@ def getelementlist():
#print "elementlist= ",datalist
return datalist



def getaddress():
recordkey="name"
recordvalue="email"
Expand All @@ -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"
Expand All @@ -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():
Expand All @@ -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())
2 changes: 1 addition & 1 deletion emailmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions start.py
Original file line number Diff line number Diff line change
Expand Up @@ -1329,17 +1329,24 @@ 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":
return redirect(url_for('show_Calibration'))

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():
Expand Down
14 changes: 13 additions & 1 deletion templates/systemmailsetting.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,19 @@

<form class="form-horizontal" action="{{ url_for('systemmailsetting') }}" method="post">
<fieldset>
<legend>System Mail Credentials</legend>
<legend>System Mail Credentials (SMTP)</legend>
<div class="form-group">
<label for="inputServer" class="col-lg-3 control-label">Server address</label>
<div class="col-lg-9">
<input type="text" class="form-control" id="inputServer" placeholder={% if server=="" %}"mail server"{% else %}"{{server}}"{% endif %} name="server" value="{{server}}">
</div>
</div>
<div class="form-group">
<label for="inputPort" class="col-lg-3 control-label">Server port</label>
<div class="col-lg-9">
<input type="text" class="form-control" id="inputPort" placeholder={% if port=="" %}"mail port"{% else %}"{{port}}"{% endif %} name="port" value="{{port}}">
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-lg-3 control-label">Mail address</label>
<div class="col-lg-9">
Expand Down

0 comments on commit 34a7c32

Please sign in to comment.