Performance: Sending simple emails
MEDIUM IMPACT
This affects the server response time and user experience during email sending operations.
import threading from flask import Flask, request from flask_mail import Mail, Message app = Flask(__name__) app.config.update( MAIL_SERVER='smtp.example.com', MAIL_PORT=587, MAIL_USE_TLS=True, MAIL_USERNAME='user@example.com', MAIL_PASSWORD='password' ) mail = Mail(app) def send_async_email(app, msg): with app.app_context(): mail.send(msg) @app.route('/send', methods=['POST']) def send_email(): msg = Message('Hello', sender='user@example.com', recipients=['recipient@example.com']) msg.body = 'This is a test email' thread = threading.Thread(target=send_async_email, args=(app, msg)) thread.start() # asynchronous call return 'Email sending started!'
from flask import Flask, request from flask_mail import Mail, Message app = Flask(__name__) app.config.update( MAIL_SERVER='smtp.example.com', MAIL_PORT=587, MAIL_USE_TLS=True, MAIL_USERNAME='user@example.com', MAIL_PASSWORD='password' ) mail = Mail(app) @app.route('/send', methods=['POST']) def send_email(): msg = Message('Hello', sender='user@example.com', recipients=['recipient@example.com']) msg.body = 'This is a test email' mail.send(msg) # synchronous call return 'Email sent!'
| Pattern | Server Blocking | Response Delay | User Interaction Delay | Verdict |
|---|---|---|---|---|
| Synchronous email sending | Blocks server thread | Delays response by 500ms+ | High INP, user waits | [X] Bad |
| Asynchronous email sending with threading | Non-blocking | Immediate response | Low INP, smooth UX | [OK] Good |