3

使用Flask-mail发送邮件

 2 years ago
source link: https://yytan02.gitee.io/blog/2018/05/22/flask-mail/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

今天尝试了一下使用 Flask-Mail 发送邮件,折腾了半天都不成功。 最后,不断的坚持终于换来了成功,分享给大家。

一、安装Flask-Mail

直接使用如下命令: pip install flask-mail

二、发送邮件的原码

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'yytan'

from flask import Flask
from flask_mail import Mail, Message

app = Flask(__name__)

app.config.update(
    # EMAIL SETTING
    MAIL_SERVER='smtp.qq.com',
    MAIL_PORT=465,
    MAIL_USE_SSL=True,
    MAIL_USERNAME='QQ_ID',
    MAIL_PASSWORD='Your_Auth_Code'
)

mail = Mail(app)

@app.route('/')
def index():
    msg = Message(subject='Hello',
                  sender='[email protected]',
                  recipients=['[email protected]'])
    msg.html = '<b>testing</b> html'

    mail.send(msg)
    return '<h1>Sent</h1>'

if __name__ == '__main__':
    app.run(debug=True)
  1. 因为我这里使用的是QQ邮箱,所以,需要登录QQ邮箱,在设置->帐户里开启IMAP/SMTP服务。这里需要用手机发送一条短信,才能获取授权码。
  2. MAIL_PASSWORD这里不是邮箱的登录密码,而是上一步获得的授权码。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK