成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Flask Web開發(fā):第六章的電子郵件配置

airborne007 / 2589人閱讀

摘要:弄了好久終于,踩了很多坑,感覺自己好菜,提供我的參考在外面設(shè)置,如,注意沒有引號(hào)和空格郵箱設(shè)置賬號(hào)獲取授權(quán)碼,在外部傳遞安全如,注意沒有引號(hào)和空格發(fā)送者郵箱接收者郵箱,,注意沒有引號(hào)參考的一個(gè)作者的文章插件系列,還有廖雪峰的教程

弄了好久終于OK,踩了很多坑,感覺自己好菜,提供我的參考

# -*- coding: utf-8 -*-
import os
from flask import Flask, render_template, session, redirect, url_for
from flask_script import Manager, Shell
from flask_bootstrap import Bootstrap
from flask_moment import Moment
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import Required
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate, MigrateCommand
from flask_mail import Mail, Message

basedir = os.path.abspath(os.path.dirname(__file__))

app = Flask(__name__)
app.config["SECRET_KEY"] = "hard to guess string"
app.config["SQLALCHEMY_DATABASE_URI"] =
    "sqlite:///" + os.path.join(basedir, "data.sqlite")
app.config["SQLALCHEMY_COMMIT_ON_TEARDOWN"] = True
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

app.config["MAIL_SERVER"] = "smtp.qq.com"
app.config["MAIL_PORT"] = 25
app.config["MAIL_USE_TLS"] = True
# 在外面設(shè)置,如:set [email protected] ,注意沒有引號(hào)和空格!
app.config["MAIL_USERNAME"] = os.environ.get("MAIL_USERNAME")
# [email protected]郵箱-->設(shè)置-->賬號(hào),獲取授權(quán)碼,在外部傳遞安全,如:set MAIL_PASSWORD=xfnftzjycypzjica,注意沒有引號(hào)和空格
app.config["MAIL_PASSWORD"] = os.environ.get("MAIL_PASSWORD")
app.config["FLASKY_MAIL_SUBJECT_PREFIX"] = "[Flasky]"
# 發(fā)送者郵箱
app.config["FLASKY_MAIL_SENDER"] = "[email protected]"
# 接收者郵箱,set FLASKY_ADMIN = [email protected],注意沒有引號(hào)
app.config["FLASKY_ADMIN"] = os.environ.get("FLASKY_ADMIN")


manager = Manager(app)
bootstrap = Bootstrap(app)
moment = Moment(app)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
mail = Mail(app)


class Role(db.Model):
    __tablename__ = "roles"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)
    users = db.relationship("User", backref="role", lazy="dynamic")

    def __repr__(self):
        return "" % self.name


class User(db.Model):
    __tablename__ = "users"
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), unique=True, index=True)
    role_id = db.Column(db.Integer, db.ForeignKey("roles.id"))

    def __repr__(self):
        return "" % self.username


def send_email(to, subject, template, **kwargs):
    msg = Message(app.config["FLASKY_MAIL_SUBJECT_PREFIX"] + " " + subject,
                  sender=app.config["FLASKY_MAIL_SENDER"], recipients=[to])
    msg.body = render_template(template + ".txt", **kwargs)
    msg.html = render_template(template + ".html", **kwargs)
    mail.send(msg)


class NameForm(FlaskForm):
    name = StringField("What is your name?", validators=[Required()])
    submit = SubmitField("Submit")


def make_shell_context():
    return dict(app=app, db=db, User=User, Role=Role)
manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command("db", MigrateCommand)


@app.errorhandler(404)
def page_not_found(e):
    return render_template("404.html"), 404


@app.errorhandler(500)
def internal_server_error(e):
    return render_template("500.html"), 500


@app.route("/", methods=["GET", "POST"])
def index():
    form = NameForm()
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.name.data).first()
        if user is None:
            user = User(username=form.name.data)
            db.session.add(user)
            session["known"] = False
            if app.config["FLASKY_ADMIN"]:
                send_email(app.config["FLASKY_ADMIN"], "New User",
                           "mail/new_user", user=user)
        else:
            session["known"] = True
        session["name"] = form.name.data
        return redirect(url_for("index"))
    return render_template("index.html", form=form, name=session.get("name"),
                           known=session.get("known", False))


if __name__ == "__main__":
    manager.run()

參考的一個(gè)作者:funhacks的文章Flask 插件系列 - Flask-Mail,還有廖雪峰的python教程

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/40678.html

相關(guān)文章

  • 《Python基礎(chǔ)教程》六章--讀書筆記

    摘要:第六章抽象本章會(huì)介紹如何將語(yǔ)句組織成函數(shù)。關(guān)鍵字參數(shù)和默認(rèn)值目前為止,我們使用的參數(shù)都是位置參數(shù),因?yàn)樗鼈兊奈恢煤苤匾?,事?shí)上比它們的名字更重要。參數(shù)前的星號(hào)將所有值放置在同一個(gè)元祖中。函數(shù)內(nèi)的變量被稱為局部變量。 第六章:抽象 本章會(huì)介紹如何將語(yǔ)句組織成函數(shù)。還會(huì)詳細(xì)介紹參數(shù)(parameter)和作用域(scope)的概念,以及遞歸的概念及其在程序中的用途。 懶惰即美德 斐波那契數(shù)...

    AnthonyHan 評(píng)論0 收藏0
  • 如果想成為一名頂尖的前端,這份書單你一定要收藏!

    摘要:其中負(fù)載均衡那一節(jié),基本上是參考的權(quán)威指南負(fù)載均衡的內(nèi)容。開發(fā)指南讀了一半,就是看這本書理解了的事件循環(huán)。哈哈創(chuàng)京東一本騙錢的書。 歡迎大家前往騰訊云+社區(qū),獲取更多騰訊海量技術(shù)實(shí)踐干貨哦~ 本文由騰訊IVWEB團(tuán)隊(duì) 發(fā)表于云+社區(qū)專欄作者:link 2014年一月以來(lái),自己接觸web前端開發(fā)已經(jīng)兩年多了,記錄一下自己前端學(xué)習(xí)路上看過的,以及道聽途說的一些書,基本上按照由淺入深來(lái)介紹...

    callmewhy 評(píng)論0 收藏0
  • 如果想成為一名頂尖的前端,這份書單你一定要收藏!

    摘要:其中負(fù)載均衡那一節(jié),基本上是參考的權(quán)威指南負(fù)載均衡的內(nèi)容。開發(fā)指南讀了一半,就是看這本書理解了的事件循環(huán)。哈哈創(chuàng)京東一本騙錢的書。 歡迎大家前往騰訊云+社區(qū),獲取更多騰訊海量技術(shù)實(shí)踐干貨哦~ 本文由騰訊IVWEB團(tuán)隊(duì) 發(fā)表于云+社區(qū)專欄作者:link 2014年一月以來(lái),自己接觸web前端開發(fā)已經(jīng)兩年多了,記錄一下自己前端學(xué)習(xí)路上看過的,以及道聽途說的一些書,基本上按照由淺入深來(lái)介紹...

    Scliang 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<