烧瓶-在特定条件下发送电子邮件

时间:2018-10-06 01:26:49

标签: python flask

我有这个脚本,只要它满足条件,它就会在其中发送电子邮件。编码有什么问题?我尝试过多次重启服务,但仍然没有结果。

感谢您的帮助。

from flask import Flask, render_template
from flask_mail import Mail, Message
#other imports


app = Flask(__name__)
app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = 'mail@gmail.com'  # enter your email here
app.config['MAIL_DEFAULT_SENDER'] = 'mail@gmail.com' # enter your email here
app.config['MAIL_PASSWORD'] = 'password' # enter your password here
mail=Mail(app)
#other parts unnecessary to be posted

@app.route("/")
def tempReading():
    t=open(temp_sensor,'r')
    lines=t.readlines()
    t.close()

    dista=subprocess.check_output('sudo python /home/pi/webserver/sonic.py', shell=True)

    temp_output = lines[1].find('t=')
    if temp_output != -1:
        temp_string=lines[1].strip()[temp_output+2:]
        temp_c=float(temp_string)/1000.0
    elif temp_c>30:
        msg=Message("Temperature Warning", recipients['1234567@sms.clicksend.com'])
        msg.body="Temperature is ",temp_c
        mail.send(msg)
    templateData = {
        'temp': round(temp_c,1),
                'dis': dista
    }
    return render_template('temp.html',**templateData)


if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80, debug=True)

1 个答案:

答案 0 :(得分:0)

我认为您只是希望它是if而不是elif

temp_c = None
if temp_output != -1:
    temp_string=lines[1].strip()[temp_output+2:]
    temp_c=float(temp_string)/1000.0
else:
    # return an error here if you can't find temp_c?
if temp_c and temp_c>30:
    msg=Message("Temperature Warning", recipients['1234567@sms.clicksend.com'])
    msg.body="Temperature is %d" % temp_c
    mail.send(msg)