I recently made a pretty awesome email library and I decided to make a Flask extension for it as well: Flask-Redmail
Install
pip install flask-redmail
Configuration
from flask import Flask
from flask_redmail import RedMail
app = Flask(__name__)
email = RedMail(app)
# Setting configs
app.config["EMAIL_HOST"] = "localhost"
app.config["EMAIL_PORT"] = 0
app.config["EMAIL_USERNAME"] = "[email protected]"
app.config["EMAIL_PASSWORD"] = "<PASSWORD>"
app.config["EMAIL_SENDER"] = "[email protected]"
Simple Example
You may send emails simply:
@app.route("/send-email")
def send_email():
email.send(
subject="Example email",
receivers=["[email protected]"],
html="""
<h1>Hi,</h1>
<p>this is an example.</p>
"""
)
Template Example
It can also use your default Jinja env. Create a file emails/verify.html to your Flask template folder that looks like this:
<h1>Hi, you visited {{ request.endpoint }}.</h1>
Then use this template:
@app.route("/send-email-template")
def send_email():
email.send(
subject="Template example",
receivers=["[email protected]"],
html_template="emails/verify.html"
)
Flask Redmail is quite a simple wrapper for Red Mail and Red Mail is pretty extensive. It can:
- Send attachments from various types
- Embed images and plots to the HTML
- Embed prettified tables to the HTML
- Supports Jinja out-of-the-box
- Gmail and Outlook are preconfigured
I hope you find it useful.
Links:
Red Mail: