当前位置:网站首页>Flask Framework - Flask-Mail Mail
Flask Framework - Flask-Mail Mail
2022-07-30 14:10:00 【White Chocolate LIN】
目录
上篇文章我们学习了Flask框架——Flask-WTF表单:文件上传、验证码,这篇文章我们学习Flask框架——Flask-Mail邮件.
WebApplications often need to communicate to the user、客户端、管理员、Operation and maintenance personnel and other relevant personnel send emails.在Flask框架中提供了Flask-MailMail library to manage the sending and receiving of e-mails.
安装Flask-Mail
执行如下代码安装Flask-Mail:
pip install flask-mail
安装后,You can execute the following code to viewFlask-Mail的信息:
pip show flask-mail
信息如下所示:
Name: Flask-Mail
Version: 0.9.1
Summary: Flask extension for sending email
Home-page: https://github.com/rduplain/flask-mail
Author: Dan Jacob
Author-email: [email protected]
License: BSD
Location: c:\users\lin\desktop\flask-mail邮件\venv\lib\site-packages
Requires: blinker, Flask
Required-by:
配置Flask-Mail
在使用Flask-Mail之前,需要进行配置.
创建Flask项目,其代码如下所示:
from flask import Flask
from flask_mail import Mail
app =Flask(__name__)
app.config['MAIL_SERVER']='smtp.qq.com' #邮件服务器的名称/IP地址
app.config['MAIL_PORT'] = 465 #所用服务器的端口号
app.config['MAIL_USERNAME'] = '[email protected]' #发件人的用户名
app.config['MAIL_PASSWORD'] = '*******' #发件人的POP3/IMAP/SMTP服务的SSLConnection client authorization code
app.config['MAIL_USE_TLS'] = False #禁用传输安全层加密
app.config['MAIL_USE_SSL'] = True #Enable Secure Sockets Layer encryption
mail = Mail(app) #Create a mail class object
if __name__ == '__main__':
app.run(debug = True)
这里我们是使用QQ邮箱作为发件邮箱.
QQMailbox related server information:
| 服务器名称 | 服务器地址 | SSL协议端口号 | 非SSL协议端口号 |
|---|---|---|---|
| IMAP | imap.qq.com | 993 | 143 |
| SMTP | smtp.qq.com | 465/587 | 25 |
| POP3 | pop.qq.com | 995 | 110 |
SSLThe connection client authorization code can be obtained as shown below:


下拉,找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务

开启POP3、IMAP/SMTP服务,发送短信,

After sending a text message, you will get the client authorization code.
Except just now we were thereFlaskparameters configured in the program,The following parameters can also be added:
MAIL_DEBUG:Support custom debugging,默认是Flask应用程序的调试状态;
MAIL_DEFAULT_SENDER:设置默认发件人;
MAIL_MAX_EMAILS:设置要发送的最大邮件;
MAIL_SUPPRESS_SEND: 如果app.testing设置为true,则发送被抑制;
MAIL_ASCII_ATTACHMENTS: 如果设置为true,则将附加的文件名转换为ASCII.
使用Flask-Mail
Here we send emails in the form of web pages.
在上面的Flask程序中,We write the view function,如下所示:
@app.route('/write')
def write_mail():
return render_template('write.html')
这里我们首先创建一个write_mail视图函数,Its role is to renderwrite.html模板文件,write.html文件,代码如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="{
{ url_for('send_mail') }}" method="post">
<p>收件人是:<input type="text" name="username"></p>
<p>Email subject is :<input type="text" name="theme"></p>
<p>Email content is:<input type="text" name="content"></p>
<p><input type="submit" value="发送"></p>
</form>
</body>
</html>
这里我们使用了url_for()method calls the view functionsend_mail(),当点击发送时,就会调用send_mail()视图函数,And pass the data entered in the text to the request.
send_mail视图函数代码如下所示:
@app.route('/send' ,methods=['GET','POST'])
def send_mail():
username=request.form.get('username') #获取请求中的username参数
theme=request.form.get('theme') #获取请求中的theme参数
content=request.form.get('content') #获取请求中的content
msg = Message(theme, sender='[email protected]', recipients=[username],body=content) #使用Messgae方法
mail.send(msg) #使用Mail类中的send()方法
return '邮件发送成功'
获取请求中的参数,再使用Message()Instance encapsulates mail,其语法结构为:
其中:The first parameter is the email subject,第二个senderThe parameter is the sender,第三个recipientsThe parameter is the recipient,Its parameter value is a list,So multiple recipients can be passed in at the same time,The fourth parameter is the email content.
最后使用Mail类中的send()方法发送邮件,
在MailThe class has the following methods:
| 方法 | 描述 |
|---|---|
| send() | 发送Message类对象的内容 |
| connect() | 与邮件主机打开连接 |
| send_message() | 发送消息对象 |
好了,代码已经写好了,启动Flask程序,访问http://127.0.0.1:5000/write,Enter the content correctly,如下图所示:

点击发送后,就会跳转到http://127.0.0.1:5000/send网页中,如下图所示:

好了,关于Flask框架——Flask-MailEmail is here,感谢观看,下篇文章我们继续学习Flask框架——Flask-SQLite数据库.
公众号:白巧克力LIN
该公众号发布Python、数据库、Linux、Flask、自动化测试、Git等相关文章!
- END -
边栏推荐
- Flask框架——Flask-Mail邮件
- 逻辑漏洞----权限类漏洞
- OFDM 十六讲 3- OFDM Waveforms
- Learning notes - 7 weeks as data analyst "in the first week: data analysis of thinking"
- The way of programmers' cultivation: do one's own responsibilities, be clear in reality - lead to the highest realm of pragmatism
- (一)Multisim安装与入门
- NFTScan 与 PANews 联合发布多链 NFT 数据分析报告
- selenium4+pyetsst+allure+pom进行自动化测试框架的最新设计
- 20220729 Securities, Finance
- Skywalking入门
猜你喜欢
随机推荐
cpu/CS and IP
js背景切换时钟js特效代码
(HR面试)最常见的面试问题和技巧性答复
sql中ddl和dml(sql与access的区别)
(一)Multisim安装与入门
[ARC092D] Two Faced Edges
自动化测试的生命周期是什么?
cpu / CS 和 IP
CF603E Pastoral Oddities
05 | login background: based on the password login mode (below)
12、 学习MySQL 排序
CF338E Optimize!
Jenkins自动化部署项目
CF1320E Treeland and Viruses
js人均寿命和GDP散点图统计样式
SQL 26 calculation under 25 years of age or older and the number of users
CF338E Optimize!
【Advanced Mathematics】【7】Double Integral
[C# 循环跳转]-C# 中的 while/do-while/for/foreach 循环结构以及 break/continue 跳转语句
新一代开源免费的终端工具,太酷了








