-sharp!/usr/bin/env python
-sharp coding:utf-8
from flask import Flask,request
from flask_mail import Mail, Message
app = Flask(__name__)
app.config["MAIL_SERVER"] = "smtp.139.com"
app.config["MAIL_PORT"] = 465
app.config["MAIL_USE_SSL"] = True
app.config["MAIL_USERNAME"] = "1119527@139.com"
app.config["MAIL_PASSWORD"] = "11111"
app.config["MAIL_DEFAULT_SENDER"] = "119527@139.com"
mail = Mail(app)
import threading
@app.route("/sendMail",methods=["POST","GET"])
def index():
title = request.args.get("title")
content = request.args.get("content")
tolist = request.args.get("tolist")
tolist = str(tolist).split(",")
status = sendmail(title,content,tolist)
return status
def send_async_email(app,msg):
with app.app_context():
mail.send(msg)
def sendmail(title,html,tolist):
msg = Message(title, recipients=tolist)
msg.html = html
-sharpmsg.body = "This is a first email"
thr = threading.Thread(target=send_async_email, args=[app,msg])-sharp
thr.start()
return thr
if __name__ == "__main__":
app.run(host="0.0.0.0",debug=True)
Code as above, the function is to use flask mail api to send mail asynchronously. When you use curl call, you will report an error.
I don"t know why Thread reported an error. The module has been verified and there is no problem. Hope for advice
curl http://127.0.0.1:5000/sendMail?title=11\&content="222"\&tolist=33@qq.com
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\flask\app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python27\lib\site-packages\flask\app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "C:\Python27\lib\site-packages\flask\app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1615, in full_dispatch_request
return self.finalize_request(rv)
File "C:\Python27\lib\site-packages\flask\app.py", line 1630, in finalize_request
response = self.make_response(rv)
File "C:\Python27\lib\site-packages\flask\app.py", line 1740, in make_response
rv = self.response_class.force_type(rv, request.environ)
File "C:\Python27\lib\site-packages\werkzeug\wrappers.py", line 921, in force_type
response = BaseResponse(*_run_wsgi_app(response, environ))
File "C:\Python27\lib\site-packages\werkzeug\wrappers.py", line 59, in _run_wsgi_app
return _run_wsgi_app(*args)
File "C:\Python27\lib\site-packages\werkzeug\test.py", line 923, in run_wsgi_app
app_rv = app(environ, start_response)
TypeError: "Thread" object is not callable