#!/usr/bin/env python
# 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
#msg.body = "This is a first email"
thr = threading.Thread(target=send_async_email, args=[app,msg])#创建线程
thr.start()
return thr
if __name__ == '__main__':
app.run(host='0.0.0.0',debug=True)
代码如上,功能是使用flask mail api异步发送邮件,使用curl 调用的时候,报错
不知道为什么Thread 报错? 模块验证过没问题。望指教
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
把全局的mail初始化删掉,在异步发送代码中初始化
这样试下呢