Performance: Email with attachments
MEDIUM IMPACT
This affects server response time and network load during email sending, impacting user wait time and server throughput.
from flask_mail import Mail, Message mail = Mail(app) msg = Message('Hello', sender='from@example.com', recipients=['to@example.com']) with open('large_file.zip', 'rb') as f: # Ideally, implement streaming or chunked reading here msg.attach('large_file.zip', 'application/zip', f.read(1024*1024)) # read in chunks or stream # Alternatively, send email asynchronously using background task from threading import Thread def send_async_email(app, msg): with app.app_context(): mail.send(msg) Thread(target=send_async_email, args=(app, msg)).start()
from flask_mail import Mail, Message mail = Mail(app) msg = Message('Hello', sender='from@example.com', recipients=['to@example.com']) with open('large_file.zip', 'rb') as f: msg.attach('large_file.zip', 'application/zip', f.read()) mail.send(msg)
| Pattern | Memory Usage | Server Blocking | Network Load | Verdict |
|---|---|---|---|---|
| Read entire file synchronously | High (loads full file in memory) | Blocks server thread | Full file sent at once | [X] Bad |
| Read file in chunks or stream | Low (small buffer size) | Non-blocking if async | Full file sent efficiently | [OK] Good |