记踩坑--Flask Web开发:第六章的电子邮件 ----[Errno 11004] getaddrinfo failed

Posted 暮良文王

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了记踩坑--Flask Web开发:第六章的电子邮件 ----[Errno 11004] getaddrinfo failed相关的知识,希望对你有一定的参考价值。

必须要记录下踩过的坑,一来,为后来者铺路,二来,实在摔得疼,提醒自己写代码要谨小慎微。

[Errno 11004] getaddrinfo failed

1.先排除邮箱账号和授权码的错误

测试如下代码:(注:邮箱号和授权码换成你自己的)

# -*- coding: utf-8 -*-

from flask import Flask
from flask_mail import Mail, Message

app = Flask(__name__)

app.config[\'MAIL_SERVER\'] = \'smtp.qq.com\'  # 邮件服务器地址
app.config[\'MAIL_PORT\'] = 25  # 邮件服务器端口
app.config[\'MAIL_USE_TLS\'] = True  # 启用 TLS
app.config[\'MAIL_USERNAME\'] = \'987654321@qq.com\'  
app.config[\'MAIL_PASSWORD\'] = \'nxvxvzbbbbbbbcec\'

mail = Mail(app)


@app.route(\'/\')
def index():
    msg = Message(\'恭喜!排除授权码和账号的问题!\', sender=\'987654321@qq.com\', recipients=[\'987654321@qq.com\'])
    msg.html = \'<b>Hello Web</b>\'
    mail.send(msg)
    return \'<h1>OK!</h1>\'


if __name__ == \'__main__\':
    app.run(host=\'127.0.0.1\', debug=True)

 

2.再排查环境变量是否设置正确

打开你当前文件的虚拟环境,设置如下代码

(venv) $ set MAIL_USERNAME = my_email@qq.com
(venv) $ set MAIL_PASSWORD = password

 注意::环境变量配置中不加引号!!不加引号!!不加引号!!

 

程序中看是否成功get到

print(os.environ.get(\'MAIL_USERNAME\'))
print(os.environ.get(\'MAIL_PASSWORD\'))

 

能get到后,就可以在程序中获取环境变量的配置啦!

app.config[\'MAIL_USERNAME\'] = os.environ.get(\'MAIL_USERNAME\') 

app.config[\'MAIL_PASSWORD\'] = os.environ.get(\'MAIL_PASSWORD\') 

 

3.在程序中集成发送电子邮件功能

import os
from flask import Flask, render_template, session, url_for, redirect
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired
from flask_sqlalchemy import SQLAlchemy
from flask_script import Manager
from flask_script import Shell
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_COMMMIT_ON_TEARDOWN\'] = True
app.config[\'SQLALCHEMY_TRACK_MODIFICATIONS\'] = True

app.config[\'MAIL_SERVER\'] = \'smtp.qq.com\'
app.config[\'MAIL_PORT\'] = 25
app.config[\'MAIL_USE_TLS\'] = True
app.config[\'MAIL_USERNAME\'] = os.environ.get(\'MAIL_USERNAME\')
print(os.environ.get(\'MAIL_USERNAME\'))
app.config[\'MAIL_PASSWORD\'] = os.environ.get(\'MAIL_PASSWORD\')
print(os.environ.get(\'MAIL_PASSWORD\'))
app.config[\'FLASKY_MAIL_SUBJECT_PREFIX\'] = \'[Flasky]\'
app.config[\'FLASKY_MAIL_SENDER\'] = \'123456789@qq.com\'  # 发送者邮箱
app.config[\'FLASKY_ADMIN\'] = os.environ.get(\'FLASKY_ADMIN\')  # 接收者邮箱
print(os.environ.get(\'FLASKY_ADMIN\'))

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


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


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 \'<Role: %s>\' % 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 \'<User: %s>\' % 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)


def make_shell_context():
    return dict(app=app, db=db, User=User, Role=Role)


manager.add_command(\'shell\', Shell(make_context=make_shell_context))  # 集成python shell
manager.add_command(\'db\', MigrateCommand)  # 创建数据库迁移


@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:
            new_user = User(username=form.name.data)
            db.session.add(new_user)
            db.session.commit()
            session[\'known\'] = False  # 新用户(数据库中没查到你)
            if app.config[\'FLASKY_ADMIN\']:
                send_email(app.config[\'FLASKY_ADMIN\'], \'New User\',
                           \'mail/new_user\', user=new_user)
        else:
            session[\'known\'] = True  # 老用户
        session[\'name\'] = form.name.data
        form.name.data = \'\'
        return redirect(url_for(\'index\'))
    return render_template(\'index.html\', form=form, name=session.get(\'name\'),
                           known=session.get(\'known\', False))


@app.route(\'/user/<name>\')
def user(name):
    return render_template(\'user.html\', name=name)


@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


if __name__ == \'__main__\':
    # manager.run()
    app.run(host=\'127.0.0.1\', port=5200, debug=True)

 

4. 创建模板

 

 new_user.html:

User {{ user.username }} has joined.

new_user.txt:

User <b>{{ user.username }}</b> has joined.

  

 5. 跑起来

(env) C:\\Users\\Administrator\\PycharmProjects\\lagou\\jokertion_blog>python hello.py shell

 

注意:记得先在虚拟环境(venv)中设置好三个环境变量:

set MAIL_USERNAME=123456789@qq.com

set MAIL_PASSWORD=xxxxxxxxxxxxxx

set FLASKY_ADMIN=123456789@qq.com

  

 6.大功告成!新增功能:新添用户,自动发送邮件提醒管理员。

 

以上是关于记踩坑--Flask Web开发:第六章的电子邮件 ----[Errno 11004] getaddrinfo failed的主要内容,如果未能解决你的问题,请参考以下文章

第六章:FLASK模版引擎以及模版方法

第六章 Flask——蓝图(BluePrint)对象

第六章心得体会

flask-mail的使用

第六章 部署Python开发的web业务

Chromium内核浏览器编译记踩坑实录