Flask-Mail ========== ![category](développement) ![tag](python) ![tag](flask) On peut avoir besoin dans une application d'envoyer des mails ```python pip install flask pip install Flask-Mail ``` on va par la suite créer cette arborescence ``` dir |__ myapp.py |__ myapp.cfg |__ templates | |__ mail.html |__ statics |__ img |__ hello.png ``` myapp.cfg ```python #!/usr/bin/env python # -*- coding: utf-8 -*- import os import logging DEBUG = True PATH_STATICS = './statics' SECRET_KEY = 'secret_key' PORT = 5001 HOST = '0.0.0.0' MAIL_SERVER = 'srvpzex1' ``` myapp.py ```python from flask import Flask, send_from_directory from flask_mail import Mail, Message mail = Mail() app = Flask(__name__) app.config.from_pyfile('myapp.cfg') mail.init_app(app) @app.route("/mail") def send_mail(): msg = Message("Hello", sender="informatiquepz@proplast-group.com", recipients=["f.aoustin@proplast-group.com"]) msg.body = "testing" with app.open_resource("%s/img/hello.png" % app.config['PATH_STATICS']) as fp: msg.attach("hello.png", "image/png", fp.read()) #msg.html = "testing" mail.send(msg) return send_from_directory(app.config['PATH_STATICS'],'mail.html') if __name__ == "__main__": app.run(host = app.config['HOST'], port = app.config['PORT']) ``` mail.html ```html send mail ``` on peut tester sur http://127.0.0.1:5001/mail