How to send emails while using Python Flask fully asynchronously ?
Tags: flask, mail
Published: 2021-09-08 08:45:42

<center>
<img src="https://i.pinimg.com/originals/97/56/c2/9756c2a05e2dd85309fe4b3bc5d62357.gif" height="300px" width="550px"></img>  
</center>

#### Introduction
__S__ending 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 :trident:

##### Official Documentation: https://marktennyson.github.io/flask-mailing/ :large_orange_diamond:

________________________________  

#### Key features of the `Flask-Mailing` module:
- :loop: Fully asynchronous support for email sending.
* :outbox_tray: sending emails with either with Flask or using asyncio module.
* :fleur_de_lis: Able to perform any `TLS` of `SSL` based encryption.
* :crossed_swords: sending files either from form-data or files from server.
* :newspaper_roll: Using Jinja2 HTML Templates.
- :adhesive_bandage: email utils (utility allows you to check temporary email addresses, you can block any email or domain).
- :magnet: email utils has two available classes DefaultChecker and WhoIsXmlApi.

More information will be available at the [Getting-Started](https://marktennyson.github.io/flask-mailing/getting-started/) section. :grinning:

#### :heart: Configure `Flask-Mailing` with a simple `Flask application`:

* :wave: install or update the `Flask-Mailing` module on your machine from `PYPI` using `PIP`
```bash
pip install -U flask-mailing
```

* :vampire_woman: Create a simple flask application, like this:
```python
from flask import Flask

app= Flask(__name__)

@app.route("/")
def index():
    return "hello from Pluto."

if __name__ == "__main__":
    app.run(debug=True)
```
* :bouncing_ball_woman: Now import the `Mail` and `Message` class from `flask-mailing` and set the app's default config variable:
```python
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)
```

* :weight_lifting_woman: Now configure a route to initialize the `Message` class to create a `message` instance.
```python
@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"})
```

* :bear: Here is the complete example for better understanding of the working procedure:
```python
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"})
```
* :panda_face: For more example plese click [here](https://marktennyson.github.io/flask-mailing/example/)

#### Support 😇:
I have created some more python `Flask` extensions including [Flask-Express](https://github.com/marktennyson/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](https://www.buymeacoffee.com/aniketsarkar) ❤️
### [Paytm] (https://paytm.me/vA-DF7O)
#### UPI: aniketsarkar@ybl

<br>

#### Special Note :red_circle:
If you would like to use Django's email implementation, you should probably go with `waynerv's` [Flask-Mailman](https://www.github.com/waynerv/Flask-Mailman) package.

<br>

`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 ❤️