烧瓶邮件显示socket.gaierror:[Errno 11001] getaddrinfo失败

时间:2020-04-06 05:04:55

标签: python flask flask-mail

我是Flask的新手。这是我编写联系表格的代码。在此项目中首次使用Blueprint。 这是我对该项目的配置。 config.py

import os

class config(object):
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'you-will-never-guess'
    SQLALCHEMY_DATABASE_URI = 'sqlite:///D:/Project/ASB/asb.db'
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    MAIL_SERVER = 'stmp.gmail.com'
    MAIL_PORT = 587
    MAIL_USE_SSL = False
    MAIL_USE_TSL = True
    MAIL_USERNAME = 'email@gmail.com'
    MAIL_PASSWORD = 'password'

这是我的主要 init 页面,用于注册蓝图和所有内容。 初始化 .py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_mail import Mail
from main import main
from config import config


app = Flask(__name__)
app.config.from_object(config)
db = SQLAlchemy(app)
mail = Mail()
mail.init_app(app)


#registering Blueprints
app.register_blueprint(main)

app.run(debug=True)

这是我联系页面的完整代码。 main.py

    from flask import Blueprint, render_template, url_for, request, flash
    from flask_mail import Mail, Message
    main = Blueprint('main', __name__)

    @main.route('/contact-us', methods=['GET', 'POST'])
    def contact_us_page():
        forms = ContactUs()
        title = 'Contact Us'
        if request.method == 'POST':
            if forms.validate_on_submit():
                mail = Mail()
                msg = Message("Subject", sender=("from@example.com"),recipients=["hello@mydomain.com"])
                msg.body = "Message"
                mail.send(msg)
            flash('All fields are Required. Fill the form Correctly')
        return render_template('contact-us.html', title = title, forms=forms)

我遇到错误。我太困惑了。 请帮助我。

socket.gaierror
socket.gaierror: [Errno 11001] getaddrinfo failed

1 个答案:

答案 0 :(得分:0)

尝试一下:

更改init.py

mail = Mail()
mail.init_app(app)

mail = Mail(app)

删除

mail = Mail()

并添加

from init import mail

在main.py