45 lines
1.1 KiB
Python
45 lines
1.1 KiB
Python
from flask import Flask, request, render_template
|
|
from flask_mail import Mail, Message
|
|
from threading import Thread
|
|
|
|
app = Flask(__name__)
|
|
|
|
app.config['MAIL_SERVER'] = 'smtp.163.com'
|
|
app.config['MAIL_PORT'] = 25
|
|
app.config['MAIL_USE_TLS'] = True
|
|
app.config['MAIL_USERNAME'] = 'fecribd@163.com'
|
|
app.config['MAIL_PASSWORD'] = 'NSBFBXRGKBKLUMMG'
|
|
|
|
mail = Mail(app)
|
|
|
|
|
|
def send_async_email(mail_app, msg):
|
|
with mail_app.app_context():
|
|
mail.send(msg)
|
|
|
|
|
|
@app.route('/send_mail', methods=['POST'])
|
|
def send_mail():
|
|
|
|
title = request.json['title']
|
|
sender = request.json['sender']
|
|
recipients = request.json['recipients']
|
|
msg_body = request.json['msg_body']
|
|
|
|
msg = Message(title, sender=sender, recipients=recipients)
|
|
msg.body = msg_body
|
|
# msg.html = render_template('email.html', user=msg_body['user'], verify_code=msg_body['verify_code'])
|
|
|
|
thread = Thread(target=send_async_email, args=[app, msg])
|
|
thread.start()
|
|
|
|
return "邮件已发送"
|
|
|
|
@app.route('/', methods=['GET'])
|
|
def index():
|
|
return {"info": "server_mail"}
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run()
|