0
0
Flaskframework~5 mins

Async email sending in Flask

Choose your learning style9 modes available
Introduction

Sending emails can take time and slow down your app. Async email sending lets your app keep working while the email is sent in the background.

When you want to send a welcome email after user signup without delay.
When sending password reset emails without making the user wait.
When sending newsletters or bulk emails without blocking the app.
When you want to improve app speed by handling email sending separately.
Syntax
Flask
from flask_mail import Message
from threading import Thread
from flask import current_app

def send_async_email(app, msg):
    with app.app_context():
        mail.send(msg)

def send_email(subject, sender, recipients, body):
    msg = Message(subject, sender=sender, recipients=recipients)
    msg.body = body
    Thread(target=send_async_email, args=(current_app._get_current_object(), msg)).start()

Use Thread to run email sending in the background.

Use app.app_context() inside the thread to access Flask app context.

Examples
Sends a simple email asynchronously using a thread.
Flask
msg = Message('Hello', sender='me@example.com', recipients=['you@example.com'])
msg.body = 'This is a test email.'
Thread(target=send_async_email, args=(app, msg)).start()
Wraps email sending in a function that runs in the background.
Flask
def send_async_email(app, msg):
    with app.app_context():
        mail.send(msg)

# Call this function to send email
send_email('Hi', 'me@example.com', ['you@example.com'], 'Async email body')
Sample Program

This Flask app sends an email asynchronously when you visit the /send route. The email sending runs in a background thread so the page loads immediately.

Flask
from flask import Flask, current_app
from flask_mail import Mail, Message
from threading import Thread

app = Flask(__name__)
app.config.update(
    MAIL_SERVER='smtp.example.com',
    MAIL_PORT=587,
    MAIL_USE_TLS=True,
    MAIL_USERNAME='your_username',
    MAIL_PASSWORD='your_password'
)
mail = Mail(app)

def send_async_email(app, msg):
    with app.app_context():
        mail.send(msg)

def send_email(subject, sender, recipients, body):
    msg = Message(subject, sender=sender, recipients=recipients)
    msg.body = body
    Thread(target=send_async_email, args=(current_app._get_current_object(), msg)).start()

@app.route('/send')
def send():
    send_email('Hello from Flask', 'me@example.com', ['you@example.com'], 'This email is sent asynchronously!')
    return 'Email is being sent in the background.'
OutputSuccess
Important Notes

Make sure your mail server settings are correct in app.config.

Using threads is simple but for heavy email loads consider task queues like Celery.

Always use app.app_context() inside threads to avoid errors.

Summary

Async email sending lets your app stay fast by sending emails in the background.

Use Python threads and Flask app context to send emails asynchronously.

Configure your mail server and call the async send function to avoid blocking.