关于email的操作:
(venv) $ pip install flask-mail
然后 :再 google的网络服务器端 ,做下自己的个性化设置 :
然后:
Example 6-1. hello.py: Flask-Mail configuration for Gmail
import os
# ...
app.config['MAIL_SERVER'] = 'smtp.googlemail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD')
然后:
Flask-Mail is initialized as shown in Example 6-2.
Example 6-2. hello.py: Flask-Mail initialization
from flask_mail import Mail
mail = Mail(app)
然后:
(venv) $ set MAIL_USERNAME=
(venv) $ set MAIL_PASSWORD=
然后:
Sending Email from the Python Shell
To test the configuration, you can start a shell session and send a test email (replace
[email protected] with your own email address):
(venv) $ flask shell
>>> from flask_mail import Message
>>> from hello import mail
>>> msg = Message('test email', sender='[email protected]',
... recipients=['[email protected]'])
>>> msg.body = 'This is the plain text body'
>>> msg.html = 'This is the HTML body'
>>> with app.app_context():
... mail.send(msg)
...
Note that Flask-Mail’s send() function uses current_app, so it needs to be executed with
an activated application context.
----------------