Skip to content
Aniket Sarkar edited this page Jan 14, 2023 · 1 revision

Flask-mailing

The flask-mailing simple lightweight mail system, sending emails and attachments(individual && bulk) fully asynchronously.

Flask_Mail is dead now. To use the mail service with your project you can use eaither Flask-Mailing for legacy or Flask-Mailman for Django type implementation.

MIT licensed GitHub stars GitHub forks GitHub issues Downloads

A Basic Demo for better understanding

from flask import Flask, jsonify
from flask_mailing import Mail, Message

mail = Mail()

def create_app():
    app = Flask(__name__)


    app.config['MAIL_USERNAME'] = "[email protected]"
    app.config['MAIL_PASSWORD'] = "world_top_secret_password"
    app.config['MAIL_PORT'] = 587
    app.config['MAIL_SERVER'] = "your-email-server.com"
    app.config['MAIL_USE_TLS'] = True
    app.config['MAIL_USE_SSL'] = False
    app.config['MAIL_DEFAULT_SENDER'] = "[email protected]"
    mail.init_app(app)

    return app

#send a simple email using flask_mailing module.

app = create_app()

@app.get("/email")
async def simple_send():

    message = Message(
        subject="Flask-Mailing module",
        recipients=["[email protected]"],
        body="This is the basic email body",
        )

    
    await mail.send_message(message)
    return jsonify(status_code=200, content={"message": "email has been sent"})

Using Jinja2 HTML Templates

In order to use Jinja template langauge, your must specify email folder within your applications working directory.

In sending HTML emails, the CSS expected by mail servers -outlook, google, etc- must be inline CSS. Flask mail passes "body" to the rendered template. In creating the template for emails the dynamic objects should be used with the assumption that the variable is named "body" and that it is a python dict.

check out jinja2 for more details jinja2

Guide for Email Utils

The utility allows you to check temporary email addresses, you can block any email or domain. You can connect to Redis to save and check email addresses. If you do not provide a Redis configuration, then the utility will save it in the list or set it by default.

Writing unittests using Flask-Mailing

Flask mailing allows you to write unittest for your application without sending emails to non existent email address by mocking the email to be sent. To mock sending out mails, set the suppress configuration to true. Suppress send defaults to False to prevent mocking within applications.

Support for Reply-To header is added

Use this just like bcc but to specify addresses that should receive a reply to your message. E-mail systems MAY respect this as per RFC 2822.