Introduction
Sending emails is one of the important work of Flask micro web framework. Now a days people are using the old and unmaintained module Flask-Mail
to send emails. Flask-Mail
is totally dead now. This is time to shift or migrate to Flask-Mailing
module to send emails fully asynchronously(The new Flask-2.0
has the asynchronous support). Flask-Mailing
module internally using the aiosmtplib
lib to provide the asynchronous SMTP connection.
Github Repo: https://github.com/marktennyson/flask-mailing đą
Official Documentation: https://marktennyson.github.io/flask-mailing/ đļ
Key features of the Flask-Mailing
module:
- âŋ Fully asynchronous support for email sending.
- đ¤ sending emails with either with Flask or using asyncio module.
- âī¸ Able to perform any
TLS
ofSSL
based encryption. - âī¸ sending files either from form-data or files from server.
- đī¸ Using Jinja2 HTML Templates.
- 𩹠email utils (utility allows you to check temporary email addresses, you can block any email or domain).
- 𧲠email utils has two available classes DefaultChecker and WhoIsXmlApi.
More information will be available at the Getting-Started section. đ
â¤ī¸ Configure Flask-Mailing
with a simple Flask application
:
- đ install or update the
Flask-Mailing
module on your machine fromPYPI
usingPIP
pip install -U flask-mailing
- đ§ââī¸ Create a simple flask application, like this:
from flask import Flask
app= Flask(__name__)
@app.route("/")
def index():
return "hello from Pluto."
if __name__ == "__main__":
app.run(debug=True)
- âšī¸ââī¸ Now import the
Mail
andMessage
class fromflask-mailing
and set the app's default config variable:
from flask_mailing import Mail, Message
app.config['MAIL_USERNAME'] = "your-email@your-domain.com"
app.config['MAIL_PASSWORD'] = "world_top_secret_password"
app.config['MAIL_PORT'] = 587
app.config['MAIL_SERVER'] = "your-email-server.com"
app.config['MAIL_TLS'] = True
app.config['MAIL_SSL'] = False
mail = Mail(app)
- đī¸ââī¸ Now configure a route to initialize the
Message
class to create amessage
instance.
@app.get("/email")
async def simple_send():
message = Message(
subject="Flask-Mailing module",
recipients=["aniketsarkar@yahoo.com"],
body="This is the basic email body",
)
await mail.send_message(message)
return jsonify(status_code=200, content={"message": "email has been sent"})
- đģ Here is the complete example for better understanding of the working procedure:
from flask import Flask, jsonify
from flask_mailing import Mail, Message
mail = Mail()
def create_app():
app = Flask(__name__)
app.config['MAIL_USERNAME'] = "your-email@your-domain.com"
app.config['MAIL_PASSWORD'] = "world_top_secret_password"
app.config['MAIL_PORT'] = 587
app.config['MAIL_SERVER'] = "your-email-server.com"
app.config['MAIL_TLS'] = True
app.config['MAIL_SSL'] = False
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=["aniketsarkar@yahoo.com"],
body="This is the basic email body",
)
await mail.send_message(message)
return jsonify(status_code=200, content={"message": "email has been sent"})
- đŧ For more example plese click here
Support đ:
I have created some more python Flask
extensions including Flask-Express, to support the Flask
as well as the Python community.
If you find this project to be useful then you can support me using the Buy Me a Coffee link below so I can continue chasing my dream of building useful Projects that will help the Python developer community and the general audience and will allow me to change my life as well đ
buy me a coffee â¤ī¸
Paytm
UPI: aniketsarkar@ybl
Special Note đ´
If you would like to use Django's email implementation, you should probably go with waynerv's
Flask-Mailman package.
Please give a star to the Github Repo of Flask-Mailing. It will help to reach out to many users
Feel free to Like and Share this post đ
Share your feedback by Commenting below đŦ
Drop me a Follow for more Awesome content related to Web Development and Programming đ
Thank you for your support â¤ī¸
Top comments (1)
I Tried to use flask_email to send otp
and provided configuration as:
MAIL_SERVER = "smtp.gmail.com"
MAIL_PORT = "465"
MAIL_USERNAME = "validemail@gmail.com"
MAIL_PASSWORD = "dummypassword"
I am facing issues where the smtp server is able to send email to @gmail and @edu accounts without any errors. But I cannot see email in my edu account from validemail@gmail.com.
I cannot think about what is wrong or what needs to be done.
can somebody please help?