from flask import Flask from flask.ext.mail import Mail, Message from threading import Thread app = Flask(__name__) app.config.update(dict( MAIL_SERVER=‘smtp.qq.com‘, MAIL_PORT=587, MAIL_USE_TLS=True, MAIL_USERNAME = ‘[email protected]‘, #use to send email MAIL_PASSWORD=‘yyy‘ #password )) mail = Mail(app) def send_async_email(msg): with app.app_context(): mail.send(msg) def send_email(): msg = Message(‘Hello, World!‘, #主题 sender=app.config[‘MAIL_USERNAME‘], #发件人 recipients=[‘[email protected]‘] #收件人 ) msg.body = "Hello" #纯文本正文 msg.html = "World" #富文本正文(测试时邮件显示这个内容) thr = Thread(target=send_async_email, args=[msg]) thr.start() return thr @app.route(‘/‘) def index(): send_email() return ‘<h1>Hello,World!</h1>‘ if __name__ == ‘__mian__‘: app.run()
时间: 2024-11-13 08:23:34