Flask Mail to send emails

- Andrés Cruz

En español
Flask Mail to send emails

Flask Mail allows you to send emails using an SMTP client previously configured in the Flask application; to install the package, we have:

$ pip install flask-mail

We will configure the SMTP server to which we are going to connect in the configuration file:


class DevConfig(Config): 
    MAIL_SERVER = 'localhost' 
    MAIL_PORT = 25 
    MAIL_USERNAME = 'username' 
    MAIL_PASSWORD = 'password' 

And in development mode, we also enable:


class DevConfig(Config):
    MAIL_DEBUG = True
    TESTING = True

We register the extension globally at the application level:


from flask_mail import Mail
mail = Mail()

You can use the following code as a reference to send emails according to the configurations made previously:

from flask_mail import Message
from my_app import mail

def send_email():
    msg = Message(body="Text Example", 
                  recipients=["andres@gmail.com"], subject="Subject")
    except Exception as e:

If you want to define HTML content:

msg = Message("Hello", sender=("Me", "me@example.com"))
msg.html = "<b>testing</b>"

Or just text:

msg = Message("Hello", sender=("Me", "me@example.com"))
msg.body = "testing"

You can get more information at:


Andrés Cruz

Desarrollo con Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz en Udemy

Acepto recibir anuncios de interes sobre este Blog.