对不起,意外的错误:'模块'对象没有属性'SMTP_SSL' [英] Sorry, unexpected error: 'module' object has no attribute 'SMTP_SSL'

查看:2540
本文介绍了对不起,意外的错误:'模块'对象没有属性'SMTP_SSL'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 这是我的main.py文件的代码,它被设计成一个简单的联系表格。从flask进口烧瓶,render_template,请求$ b $从flask_mail进口邮件,消息
从窗体import ContactForm


app = Flask(__ name__)
app。 secret_key ='YourSuperSecreteKey'

#添加邮件服务器配置
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'
$ b $ mail $ = b




$ hello():
Return a friendly HTTP greeting。
return'Hello World!'


@ app.errorhandler(404)
def page_not_found(e):
返回一个自定义的404错误。
return'对不起,这个URL没有。',404


@ app.errorhandl er(500)
def application_error(e):
返回一个自定义500错误。
return'对不起,意外的错误:格式(e), 500

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

if request.method =='POST':
if form.validate()==假:
return'请填写所有字段< p>< a href =/ contact>再次尝试!!!< / a>< / p>'
其他:
msg =留言(来自您的访客的留言+ form.name.data,
sender ='YourUser @ NameHere',
recipients = ['yourRecieve@mail.com','someOther@mail.com'])
msg.body =
来自:%s<%s>,
%s
%(form.name.data,form.email.data,form.message.data)
邮件。发送(消息)
返回成功发送消息!
elif request.method =='GET':
返回render_template('contact.html',form = form)

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

我得到错误:对不起,意外的错误:'module'object has no attribute'SMTP_SSL'



我调用了我的文件main.py。一切正常,直到我尝试发送实际的电子邮件。这是因为我没有填充设置,或者是其他的一个小姐?



对不起,刚才想到了如何在GAE上看到回溯:

 例外/联系[POST] 
追踪(最近的最后一次呼叫):
文件/base/data/home/apps/s~smart-cove-95709/1.384663697853252774/lib/flask/app。 py,第1817行,在wsgi_app
response = self.full_dispatch_request()
文件/base/data/home/apps/s~smart-cove-95709/1.384663697853252774/lib/flask/app。 py,第1477行,在full_dispatch_request
rv = self.handle_user_exception(e)
文件/base/data/home/apps/s~smart-cove-95709/1.384663697853252774/lib/flask/app .py,第1381行,在handle_user_exception
reraise(exc_type,exc_value,tb)
文件/base/data/home/apps/s~smart-cove-95709/1.384663697853252774/lib/flask/ app.py,第1475行,在full_dispatch_request
rv = self.dispatch_request()
文件/base/data/home/apps/s~smart-cove-95709/1.384663697853252774/lib/flask/ app.py,第1461行,在dispatch_request
返回self.view_functio ns [rule.endpoint](** req.view_args)
文件/base/data/home/apps/s~smart-cove-95709/1.384663697853252774/main.py,行50,联系人
mail.send(msg)
文件/base/data/home/apps/s~smart-cove-95709/1.384663697853252774/lib/flask_mail.py,第491行,发送
self.connect()作为连接:
文件/base/data/home/apps/s~smart-cove-95709/1.384663697853252774/lib/flask_mail.py,第144行,在__enter__
自身.host = self.configure_host()
文件/base/data/home/apps/s~smart-cove-95709/1.384663697853252774/lib/flask_mail.py,第156行,在configure_host
host = smtplib.SMTP_SSL(self.mail.server,self.mail.port)
AttributeError:'module'对象没有属性'SMTP_SSL'


解决方案

您已将 MAIL_USE_SSL 选项设置为 / code>:

  app.config ['MAIL_USE_SSL'] = True 

这意味着Flask-Mail扩展将要使用 smtplib.SMTP_SSL class ,但该类通常不可用于Google App Engine。



该类仅为如果Python ssl 模块< a>是可用的,如果您的Python是使用可用的SSL支持构建的,则只有这种情况。这在GAE环境中通常不是这种情况。



除非您专门启用它。在你的 app.yaml 中启用它:
$ b $ $ p $ libraries
- name:ssl
version:latest

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

在GAE上发送电子邮件最好使用 mail.send_mail()函数,您可以改用GAE基础设施。


This is my code for my main.py file which is designed to be a simple contact form built in 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()

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

I've called my file "main.py". Everything works fine until I try and send the actual email. Is this just because I haven't populated the settings or is something else a miss?

Sorry just figured out how to see traceback on 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'

解决方案

You have set the MAIL_USE_SSL option to True:

app.config['MAIL_USE_SSL'] = True

which means that the Flask-Mail extension will want to use the smtplib.SMTP_SSL class, but that class isn't usually available on Google App Engine.

That class is only available if the Python ssl module is available, which is only the case if your Python was built with SSL support available. This isn't normally the case in the GAE environment.

The module is not available on Google App Engine unless you specifically enable it. Enable it in your app.yaml:

libraries:
- name: ssl
  version: latest

Do note that the socket support on GAE is experimental.

For sending email on GAE you are better off using the mail.send_mail() function however, so you can make use of the GAE infrastructure instead.

这篇关于对不起,意外的错误:'模块'对象没有属性'SMTP_SSL'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆