python flask 中发送邮件用线程进行发送

from  flask  import  Flask

from flask_mail import Mail, Message

from threading import Thread

app = Flask(__name__)

app.config["MAIL_SERVER"] = " s m t p .163.com " 代理邮箱服务器的名字

app.config['MAIL_PORT'] = 465  #端口

app.config['MAIL_USE _  SSL'] = True

app.config['MAIL_USERNAME'] = "good [email protected]" #发送者的邮箱

app.config['MAIL_PASSWORD'] = "  "#授权码

app.config['MAIL_DEFAULT_SENDER'] =  'Flask Admin '#默认的发送邮件的地址

mail = Mail(app)


def a s y c_send_mail(message):

# 新线程里面是没有开启应用上下文的

# 下面这句代码是手动开启应用上下文

with app.app_context():

mail.send(message)


@app.route('/')

def index():

return'发送邮件'

@app.route('/send_mail')

def send_mail():

message = Message('邮件主题', recipients=['[email protected]',])#发送给谁可以给多个邮箱进行发送 message.html="

哈哈

"

thread = Thread(target=asyc_send_mail, args=(message,))

thread.start()

return '发送中'

if __name__ == '__main__':

app.run(debug=True ,port=5001)

你可能感兴趣的:(python flask 中发送邮件用线程进行发送)