SoFunction
Updated on 2024-11-10

Flask-Mail Usage Example Analysis

This article example describes Flask-Mail usage. Shared for your reference, as follows:

Many types of applications need to alert users when a specific event occurs, and a common method of communication is email.

Although the Python standard library'ssmtplib package can be used to send emails in a Flask program, but wrapping thesmtplib (used form a nominal expression)

Flask-Mail Extensions for better integration with Flask

mounting

pip3 install flask-mail

Flask-Mail SMTP Server Configuration

configure default value clarification
MAIL_SERVER localhost Hostname or IP address of the e-mail server
MAIL_PORT 25 Port of the e-mail server
MAIL_USE_TLS False Enabling Transport Layer Security Protocol
MAIL_USE_SSL False Enable Secure Sockets Layer protocol
MAIL_USERNAME None Username of the e-mail account
MAIL_PASSWORD None Password for mail account

code example

from threading import Thread
from flask import Flask
from flask_mail import Mail,Message
app = Flask(__name__)
["MAIL_SERVER"] = "smtp."
["MAIL_PORT"] = 465
["MAIL_USE_SSL"] = True
["MAIL_USERNAME"] = "xxxxx@"
["MAIL_PASSWORD"] = "123456"
mail = Mail(app)
@("/send_mail")
def send_mail():
  """
  Send Mail
  """
  message = Message("Title.",sender=["USERNAME"],recipients=["xxxxxx@"])
   = "Content"
  t = Thread(target=send_email,args=(message,))
  ()
  return "Sent successfully."
def send_email(msg):
  with app.app_context():
    (msg)
if __name__ == "__main__":
  ()

Message:: Mail messages.sender: Sender's email;recipients: Recipient email, list format;body: Content

NetEase 163 Free Email Related Server Information

When using NetEase mailbox as the sender's mailbox, you should note that the mailbox password is not the login password of the mailbox, but the client authorization password.

I hope that the description of this article will be helpful for you to design Python programs based on flask framework.