emailhelper-api-v0.1/app.py

40 lines
899 B
Python

from flask import Flask, request
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
thread = Thread(target=send_async_email, args=[app, msg])
thread.start()
return "邮件已发送"
if __name__ == '__main__':
app.run()