Sorry, unexpected error: 'module' object has no attribute 'SMTP_SSL'

Sorry, unexpected error: 'module' object has no attribute 'SMTP_SSL'

这是我的 main.py 文件的代码,它被设计成一个内置在 flask 中的简单联系表单。

from flask import Flask, render_template, request
from flask_mail import Mail, Message
from forms import ContactForm


app = Flask(__name__)
app.secret_key = 'YourSuperSecreteKey'

# add mail server config
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = 'YourUser@NameHere'
app.config['MAIL_PASSWORD'] = 'yourMailPassword'

mail = Mail(app)

@app.route('/')
def hello():
    """Return a friendly HTTP greeting."""
    return 'Hello World!'


@app.errorhandler(404)
def page_not_found(e):
    """Return a custom 404 error."""
    return 'Sorry, Nothing at this URL.', 404


@app.errorhandler(500)
def application_error(e):
    """Return a custom 500 error."""
    return 'Sorry, unexpected error: {}'.format(e), 500

@app.route('/contact', methods=('GET', 'POST'))
def contact():
    form = ContactForm()

    if request.method == 'POST':
        if form.validate() == False:
            return 'Please fill in all fields <p><a href="/contact">Try Again!!!</a></p>'
        else:
            msg = Message("Message from your visitor" + form.name.data,
                          sender='YourUser@NameHere',
                          recipients=['yourRecieve@mail.com', 'someOther@mail.com'])
            msg.body = """
            From: %s <%s>,
            %s
            """ % (form.name.data, form.email.data, form.message.data)
            mail.send(msg)
            return "Successfully  sent message!"
    elif request.method == 'GET':
        return render_template('contact.html', form=form)

if __name__ == '__main__':
    app.run()

我收到错误:Sorry, unexpected error: 'module' object has no attribute 'SMTP_SSL'

我已将我的文件命名为 "main.py"。一切正常,直到我尝试发送实际的电子邮件。这仅仅是因为我没有填充设置还是其他原因?

抱歉刚刚弄清楚如何在 GAE 上查看回溯:

Exception on /contact [POST]
Traceback (most recent call last):
  File "/base/data/home/apps/s~smart-cove-95709/1.384663697853252774/lib/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/base/data/home/apps/s~smart-cove-95709/1.384663697853252774/lib/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/base/data/home/apps/s~smart-cove-95709/1.384663697853252774/lib/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/base/data/home/apps/s~smart-cove-95709/1.384663697853252774/lib/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/base/data/home/apps/s~smart-cove-95709/1.384663697853252774/lib/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/base/data/home/apps/s~smart-cove-95709/1.384663697853252774/main.py", line 50, in contact
    mail.send(msg)
  File "/base/data/home/apps/s~smart-cove-95709/1.384663697853252774/lib/flask_mail.py", line 491, in send
    with self.connect() as connection:
  File "/base/data/home/apps/s~smart-cove-95709/1.384663697853252774/lib/flask_mail.py", line 144, in __enter__
    self.host = self.configure_host()
  File "/base/data/home/apps/s~smart-cove-95709/1.384663697853252774/lib/flask_mail.py", line 156, in configure_host
    host = smtplib.SMTP_SSL(self.mail.server, self.mail.port)
AttributeError: 'module' object has no attribute 'SMTP_SSL'

您已将 MAIL_USE_SSL 选项设置为 True:

app.config['MAIL_USE_SSL'] = True

这意味着 Flask-Mail 扩展将 want to use the smtplib.SMTP_SSL class,但是 class 通常在 Google App Engine 上不可用。

class 仅在 Python ssl module 可用时可用,只有当您的 Python 构建时支持可用的 SSL 时才会出现这种情况。在 GAE 环境中通常不是这种情况。

该模块在 Google App Engine 上不可用,除非您 specifically enable it。在您的 app.yaml:

中启用它
libraries:
- name: ssl
  version: latest

请注意,GAE 上的套接字支持是实验性的。

然而,要在 GAE 上发送电子邮件,您最好使用 mail.send_mail() 功能,这样您就可以改用 GAE 基础设施。