当前位置:网站首页>Flash send email
Flash send email
2022-07-24 23:45:00 【Clean night mortal dust】
Flask mail
be based on web Applications often need to have the ability to communicate to users / Function of sending mail by client .
Flask-Mail The extension makes it very easy to establish a simple interface with any e-mail server .
First , belong pip Install with the help of the utility Flask-Mail Expand .
pip3 install Flask-Mail
Then you need to configure it by setting the values of the following application parameters Flask-Mail.
| Method | describe |
|---|---|
| MAIL_SERVER | The name of the e-mail server /IP Address |
| MAIL_PORT | The port number of the server used |
| MAIL_PORT | Enable / Disable transport security layer encryption |
| MAIL_USE_SSL | Enable / Disable secure socket layer encryption |
| MAIL_DEBUG | Debugging support . The default value is Flask Debug state of the application |
| MAIL_USERNAME | The user name of the sender |
| MAIL_PASSWORD | The sender's password |
| MAIL_DEFAULT_SENDER | Set the default sender |
| MAIL_MAX_EMAILS | Set the maximum number of messages to send |
| MAIL_SUPPRESS_SEND | If app.testing Set to true, Then the transmission is suppressed |
| MAIL_ASCII_ATTACHMENTS | If set to true, Then the attached file name will be converted to ASCII |
flask-mail Modules contain definitions of the following important classes .
| Method | describe |
|---|---|
| send() | send out Message The content of the class object |
| connect() | Open the connection to the mail host |
| send_message() | Send message to |
Message class
It encapsulates an email .Message The 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 method
attach() - Add an attachment to the message . This method uses the following parameters :
filename - The name of the file to attach
content_type - MIME Files of type
data - Raw file data
Management - Content handling ( If any ).add_recipient() - Add another recipient to the message
Use Flask-Mail
step 1 - In code from flask-mail Module import Mail and Message class .
from flask_mail import Mail, Message
step 2 - Then configure... According to the following settings Flask-Mail.
app.config['MAIL_SERVER'] = 'smtp.exmail.qq.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = '***@***.io'
app.config['MAIL_PASSWORD'] = '******'
app.config['MAIL_USE_SSL'] = True
step 3 - establish Mail Class .
mail = Mail(app)
step 4 - In by URL The rules (‘/’) The mapping of Python Function Message object .
@app.route("/")
def index():
msg = Message('Hello', sender='**@**.com', recipients=['**@**.com'])
msg.body = "This is the email body"
mail.send(msg)
return "Sent"
step 5 - stay python Use in a function attach() Method
with app.open_resource('upload/flask.png') as f:
msg.attach('flask.png', 'image/png', f.read())
complete python Code
from flask import Flask
from flask_mail import Mail, Message
app = Flask(__name__)
mail = Mail(app)
app.config['MAIL_SERVER'] = 'smtp.exmail.qq.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = '***@***.io'
app.config['MAIL_PASSWORD'] = '******'
app.config['MAIL_USE_SSL'] = True
mail = Mail(app)
@app.route("/")
def index():
msg = Message('Hello', sender='**@**.com', recipients=['**@**.com'])
msg.body = "Hello Flask message sent from Flask-Mail"
# Add attachments
with app.open_resource('upload/flask.png') as f:
msg.attach('flask.png', 'image/png', f.read())
mail.send(msg)
return "Sent"
if __name__ == '__main__':
app.run(debug=True)
边栏推荐
- 来自大佬洗礼!2022 头条首发纯手打 MySQL 高级进阶笔记, 吃透 P7 有望
- Piziheng embedded: the method of making source code into lib Library under MCU Xpress IDE and its difference with IAR and MDK
- Notes of Teacher Li Hongyi's 2020 in-depth learning series 8
- Shardingsphere database sub database sub table introduction
- Video chat source code - one-to-one live broadcast system source code
- Solve the problem that JSP cannot use session.getattribute()
- Wine wechat initialization 96% stuck
- 50 places are limited to open | with the news of oceanbase's annual press conference coming!
- 买收益百分之6的理财产品,需要开户吗?
- Efficiency increased by 98%! AI weapon behind operation and maintenance inspection of high altitude photovoltaic power station
猜你喜欢

C language program environment and preprocessing

Go基础笔记_4_map

郑慧娟:基于统一大市场的数据资产应用场景与评估方法研究

Value driven provides the core driving force for the transformation of commercial BP - BP realization under business scenarios - Commercial BP sharing

Notes of Teacher Li Hongyi's 2020 in-depth learning series 2

Are you still using system. Currenttimemillis()? Take a look at stopwatch

How to propose effective solutions for high-end products? (1 methodology + 2 cases + 1 List)

Pit record: typeerror:'module'object is not callable

Network Security Learning (III) basic DOS commands

Network Security Learning (V) DHCP
随机推荐
Shell调试Debug的三种方式
VGA display based on FPGA
Notes of Teacher Li Hongyi's 2020 in-depth learning series 7
Let me introduce you to the partition automatic management of data warehouse
WP wechat export chat record backup to computer
NVIDIA inspector detailed instructions
ShardingSphere-数据库分库分表简介
Notes of Teacher Li Hongyi's 2020 in-depth learning series 4
Qt | 事件系统 QEvent
Cross entropy loss
Coding builds an image, inherits the self built basic image, and reports an error unauthorized: invalid credential Please confirm that you have entered the correct user name and password.
Network Security Learning (III) basic DOS commands
With screen and nohup running, there is no need to worry about deep learning code anymore | exiting the terminal will not affect the operation of server program code
Lidar obstacle detection and tracking: CUDA European clustering
The new version of SSM video tutorial in shangsilicon valley was released
痞子衡嵌入式:MCUXpresso IDE下将源码制作成Lib库方法及其与IAR,MDK差异
做一个文艺的测试/开发程序员,慢慢改变自己......
Weekly summary (*66): next five years
Routing policy in republishing
必会面试题:1.浅拷贝和深拷贝_深拷贝