Flask 简明教程
Flask – Mail
一个基于网络的应用程序常常需要一个向用户/客户端发送邮件的功能。 Flask-Mail 扩展使得使用任何电子邮件服务器设置一个简单的接口变得非常容易。
A web based application is often required to have a feature of sending mail to the users/clients. Flask-Mail extension makes it very easy to set up a simple interface with any email server.
首先,应该使用 pip 实用程序安装 Flask-Mail 扩展。
At first, Flask-Mail extension should be installed with the help of pip utility.
pip install Flask-Mail
然后,需要通过设置以下应用程序参数的值来配置 Flask-Mail。
Then Flask-Mail needs to be configured by setting values of the following application parameters.
Sr.No |
Parameters & Description |
1 |
MAIL_SERVER Name/IP address of email server |
2 |
MAIL_PORT Port number of server used |
3 |
MAIL_USE_TLS Enable/disable Transport Security Layer encryption |
4 |
MAIL_USE_SSL Enable/disable Secure Sockets Layer encryption |
5 |
MAIL_DEBUG Debug support. Default is Flask application’s debug status |
6 |
MAIL_USERNAME User name of sender |
7 |
MAIL_PASSWORD password of sender |
8 |
MAIL_DEFAULT_SENDER sets default sender |
9 |
MAIL_MAX_EMAILS Sets maximum mails to be sent |
10 |
MAIL_SUPPRESS_SEND Sending suppressed if app.testing set to true |
11 |
MAIL_ASCII_ATTACHMENTS If set to true, attached filenames converted to ASCII |
flask-mail 模块包含以下重要类的定义。
The flask-mail module contains definitions of the following important classes.
Mail class
它管理电子邮件消息传递的需求。该类的构造器采用以下形式 −
It manages email-messaging requirements. The class constructor takes the following form −
flask-mail.Mail(app = None)
构造器以 Flask 应用程序对象作为参数。
The Constructor takes the Flask application object as a parameter.
Methods of Mail class
Sr.No |
Methods & Description |
1 |
send() Sends contents of Message class object |
2 |
connect() Opens connection with mail host |
3 |
send_message() Sends message object |
Message class
它封装了一封电子邮件。Message 类的构造器有几个参数 −
It encapsulates an email message. Message class constructor has several parameters −
flask-mail.Message(subject, recipients, body, html, sender, cc, bcc,
reply-to, date, charset, extra_headers, mail_options, rcpt_options)
Message class methods
attach() − 向邮件添加附件。此方法采用以下参数 −
attach() − adds an attachment to message. This method takes the following parameters −
-
filename − name of file to attach
-
content_type − MIME type of file
-
data − raw file data
-
disposition − content disposition, if any.
add_recipient() − 向电子邮件添加另一位收件人
add_recipient() − adds another recipient to message
在以下示例中,将 Google Gmail 服务的 SMTP 服务器用作 Flask-Mail 配置的 MAIL_SERVER。
In the following example, SMTP server of Google’s gmail service is used as MAIL_SERVER for Flask-Mail configuration.
Step 1 − 在代码中从 flask-mail 模块导入 Mail 和 Message 类。
Step 1 − Import Mail and Message class from flask-mail module in the code.
from flask_mail import Mail, Message
Step 2 − 然后根据以下设置配置 Flask-Mail。
Step 2 − Then Flask-Mail is configured as per following settings.
app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'yourId@gmail.com'
app.config['MAIL_PASSWORD'] = '*****'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
Step 3 − 创建 Mail 类的一个实例。
Step 3 − Create an instance of Mail class.
mail = Mail(app)
Step 4 − 在由 URL 规则 (‘/’) 映射的 Python 函数中设置一个 Message 对象。
Step 4 − Set up a Message object in a Python function mapped by URL rule (‘/’).
@app.route("/")
def index():
msg = Message('Hello', sender = 'yourId@gmail.com', recipients = ['id1@gmail.com'])
msg.body = "This is the email body"
mail.send(msg)
return "Sent"
Step 5 − 整个代码如下。在 Python Shell 中运行以下脚本并访问 http://localhost:5000/.
Step 5 − The entire code is given below. Run the following script in Python Shell and visit http://localhost:5000/.
from flask import Flask
from flask_mail import Mail, Message
app =Flask(__name__)
mail=Mail(app)
app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'yourId@gmail.com'
app.config['MAIL_PASSWORD'] = '*****'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
mail = Mail(app)
@app.route("/")
def index():
msg = Message('Hello', sender = 'yourId@gmail.com', recipients = ['id1@gmail.com'])
msg.body = "Hello Flask message sent from Flask-Mail"
mail.send(msg)
return "Sent"
if __name__ == '__main__':
app.run(debug = True)
请注意,Gmail 服务中的内置安全功能可能会阻止此登录尝试。您可能需要降低安全级别。请登录您的 Gmail 帐户并访问 this 链接以降低安全性。
Note that the built-insecurity features in Gmail service may block this login attempt. You may have to decrease the security level. Please log in to your Gmail account and visit this link to decrease the security.