记踩坑--Flask Web开发:S6电子邮件 ----[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'] = '[email protected]'  
app.config['MAIL_PASSWORD'] = 'nxvxvzbbbbbbbcec'

mail = Mail(app)


@app.route('/')
def index():
    msg = Message('恭喜!排除授权码和账号的问题!', sender='[email protected]', recipients=['[email protected]'])
    msg.html = 'Hello Web'
    mail.send(msg)
    return '

OK!

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

 

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

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

(venv) $ set MAIL_USERNAME = [email protected]
(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'] = '[email protected]'  # 发送者邮箱
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 '' % 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)


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/')
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 {{ user.username }} has joined.

  

 5. 跑起来

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

 

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

set [email protected]

set MAIL_PASSWORD=xxxxxxxxxxxxxx

set [email protected]

  

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

记踩坑--Flask Web开发:S6电子邮件 ----[Errno 11004] getaddrinfo failed_第1张图片

记踩坑--Flask Web开发:S6电子邮件 ----[Errno 11004] getaddrinfo failed_第2张图片

 

你可能感兴趣的:(记踩坑--Flask Web开发:S6电子邮件 ----[Errno 11004] getaddrinfo failed)