Navigation

NOTE: Due to Flask’s threaded nature it’s important to wrap
envelopes.conn.SMTP object in connection stack.

fromenvelopesimportEnvelope,SMTPimportenvelopes.connstackfromflaskimportFlask,jsonifyimportosapp=Flask(__name__)app.config['DEBUG']=Trueconn=SMTP('127.0.0.1',1025)@app.before_requestdefapp_before_request():envelopes.connstack.push_connection(conn)@app.after_requestdefapp_after_request(response):envelopes.connstack.pop_connection()returnresponse@app.route('/mail',methods=['POST'])defpost_mail():envelope=Envelope(from_addr='%s@localhost'%os.getlogin(),to_addr='%s@localhost'%os.getlogin(),subject='Envelopes in Flask demo',text_body="I'm a helicopter!")smtp=envelopes.connstack.get_current_connection()smtp.send(envelope)returnjsonify(dict(status='ok'))if__name__=='__main__':app.run()