0

这是我的 main.py 文件的代码,该文件旨在成为烧瓶中内置的简单联系表单。

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'
4

1 回答 1

4

您已将MAIL_USE_SSL选项设置为True

app.config['MAIL_USE_SSL'] = True

这意味着 Flask-Mail 扩展需要使用smtplib.SMTP_SSLclass,但 Google App Engine 上通常不提供该类。

仅当 Pythonssl模块可用时,该类才可用,仅当您的 Python 是使用可用的 SSL 支持构建时才可用。在 GAE 环境中通常不是这种情况。

该模块在 Google App Engine 上不可用,除非您特别启用它。在你的启用它app.yaml

libraries:
- name: ssl
  version: latest

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

但是,要在 GAE 上发送电子邮件,最好使用该mail.send_mail()功能,因此您可以改用 GAE 基础设施。

于 2015-05-30T11:20:07.087 回答