0
0
Flaskframework~8 mins

Sending simple emails in Flask - Performance & Optimization

Choose your learning style9 modes available
Performance: Sending simple emails
MEDIUM IMPACT
This affects the server response time and user experience during email sending operations.
Sending an email when a user submits a form
Flask
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!'
Sending email in a separate thread frees the server to respond immediately, improving user experience.
📈 Performance GainNon-blocking response, reduces INP by hundreds of milliseconds to seconds.
Sending an email when a user submits a form
Flask
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!'
The mail.send() call blocks the server until the email is sent, delaying the HTTP response and making the user wait.
📉 Performance CostBlocks server response for 500ms to several seconds depending on SMTP latency, increasing INP.
Performance Comparison
PatternServer BlockingResponse DelayUser Interaction DelayVerdict
Synchronous email sendingBlocks server threadDelays response by 500ms+High INP, user waits[X] Bad
Asynchronous email sending with threadingNon-blockingImmediate responseLow INP, smooth UX[OK] Good
Rendering Pipeline
Email sending in Flask affects server-side processing before response is sent. Synchronous sending blocks the server, delaying response and user interaction. Asynchronous sending offloads email sending to a background thread, allowing faster response.
Server Processing
Response Time
User Interaction
⚠️ BottleneckServer Processing during synchronous email sending
Core Web Vital Affected
INP
This affects the server response time and user experience during email sending operations.
Optimization Tips
1Avoid synchronous email sending in request handlers to prevent blocking server response.
2Use background threads or task queues to send emails asynchronously.
3Monitor server response times in DevTools Network tab to detect blocking operations.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance problem with sending emails synchronously in a Flask route?
AIt causes layout shifts on the page
BIt increases CSS rendering time
CIt blocks the server response, increasing user wait time
DIt reduces the size of the email
DevTools: Network
How to check: Open DevTools, go to Network tab, submit the form that triggers email sending, and observe the response time of the request.
What to look for: Long response time indicates synchronous blocking; short response time with background email sending indicates good performance.