用python写一段代码,flask的blueprint 异步发送带附件的邮件
时间: 2023-04-08 14:01:48 浏览: 187
以下是使用 Python 和 Flask 的 Blueprint 异步发送带附件的邮件的代码示例:
```python
from flask import Blueprint, current_app, jsonify, request
from flask_mail import Message, Mail
import threading
mail_bp = Blueprint('mail', __name__)
mail = Mail()
def send_async_email(app, msg):
with app.app_context():
mail.send(msg)
@mail_bp.route('/send_mail', methods=['POST'])
def send_mail():
data = request.get_json()
recipient = data.get('recipient')
subject = data.get('subject')
body = data.get('body')
attachment = data.get('attachment')
msg = Message(subject=subject, recipients=[recipient])
msg.body = body
with current_app.open_resource(attachment) as f:
msg.attach(attachment, 'application/octet-stream', f.read())
thread = threading.Thread(target=send_async_email, args=(current_app._get_current_object(), msg))
thread.start()
return jsonify({'message': 'Email sent successfully.'}), 200
```
请注意,此代码示例仅供参考。在实际使用中,您需要根据您的具体需求进行修改和调整。
阅读全文