To send emails, we need to install the Flask-Mail flask extension using the below-given command.
pip install Flask-Mail
Once installed, then we need to use Flask Config API to configure MAIL-SERVER, MAIL_PORT, MAIL_USERNAME, MAIL_PASSWORD, etc. Then we need to import Message Class, instantiate it and form a message object before sending the email by using the mail.send() method.
An example is shown below.
from flask_mail import Mail, Message
from flask import Flask
app = Flask(__name__)
mail = Mail(app)
@app.route(“/mail”)
def email():
msg = Message( “Hello Message”, sender=”[email protected]”, recipients=[“[email protected]”])
mail.send(msg)