当前位置:网站首页>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)
边栏推荐
- How to put long links into Excel
- 来自大佬洗礼!2022 头条首发纯手打 MySQL 高级进阶笔记, 吃透 P7 有望
- 中金证券新客理财产品收益可以达到百分之六?开户怎么开?
- Bug summary
- Browser cache
- P3201 [hnoi2009] dream pudding heuristic merge
- See project code Note 1
- [nuxt 3] (x) runtime configuration
- Network Security Learning (IV) user and group management, NTFS
- Opengauss kernel analysis: query rewriting
猜你喜欢

Power consumption of chip

Processing PDF and JPG files in VB6

Go基础笔记_4_map

The new version of SSM video tutorial in shangsilicon valley was released

Network Security Learning (III) basic DOS commands

每周小结(*66):下一个五年

Network Security Learning (V) DHCP

来自大佬洗礼!2022 头条首发纯手打 MySQL 高级进阶笔记, 吃透 P7 有望

Add a little surprise to life and be a prototype designer of creative life -- sharing with X contestants in the programming challenge

Notes of Teacher Li Hongyi's 2020 in-depth learning series 8
随机推荐
Power consumption of chip
Go basic notes_ 4_ map
剖析kubernetes集群内部DNS解析原理
芯片的功耗
Js----- Chapter 4 array
你还在使用System.currentTimeMillis()?来看看StopWatch吧
yolov5
云计算三类巨头:IaaS、PaaS、SaaS,分别是什么意思,应用场景是什么?
Install Kaspersky 2018 under win server 2012 R2
Notes of Teacher Li Hongyi's 2020 in-depth learning series 8
Go基础笔记_4_map
Notes of Teacher Li Hongyi's 2020 in-depth learning series 9
Can the income of CICC securities' new customer financial products reach 6%? How to open an account?
多线程&高并发(全网最新:面试题 + 导图 + 笔记)面试手稳心不慌
这个月 google的“AI具备意识”的事件的想法
Add a little surprise to life and be a prototype designer of creative life -- sharing with X contestants in the programming challenge
.net redis client newlife.redis.core library usage
How to propose effective solutions for high-end products? (1 methodology + 2 cases + 1 List)
每周小结(*66):下一个五年
What are the meanings and application scenarios of the three giants of cloud computing: IAAs, PAAS and SaaS?