Flask电子邮件支持 - 发送电子邮件时获取SSLError [Errno 1]

时间:2014-12-26 19:28:37

标签: python email python-2.7 ssl flask

我正在尝试为Flask应用设置电子邮件服务器。我得到了SSLError。这是我第一次学习任何类型的Web开发。

以下是我配置服务器的方法:

# mail server settings
MAIL_SERVER = 'smtp.mail.yahoo.com'
MAIL_PORT = 587
MAIL_USE_TLS = False
MAIL_USE_SSL = True
MAIL_USERNAME = 'email@yahoo.com'
MAIL_PASSWORD = 'password'


# administrator list
ADMINS = ['email@yahoo.com']

以下是处理邮件的 init 脚本的一部分:

app.config.from_object('config')
mail = Mail(app)

以下是我尝试发送电子邮件时的错误回溯:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/Users/user/microblog/flask/lib/python2.7/site-packages/flask_mail.py", line 491, in send
    with self.connect() as connection:
  File "/Users/user/microblog/flask/lib/python2.7/site-packages/flask_mail.py", line 144, in __enter__
    self.host = self.configure_host()
  File "/Users/user/microblog/flask/lib/python2.7/site-packages/flask_mail.py", line 156, in configure_host
    host = smtplib.SMTP_SSL(self.mail.server, self.mail.port)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 777, in __init__
    SMTP.__init__(self, host, port, local_hostname, timeout)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 250, in __init__
    (code, msg) = self.connect(host, port)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 310, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 783, in _get_socket
    new_socket = ssl.wrap_socket(new_socket, self.keyfile, self.certfile)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 387, in wrap_socket
    ciphers=ciphers)    
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 143, in __init__
    self.do_handshake()    
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 305, in do_handshake
    self._sslobj.do_handshake()   
ssl.SSLError: [Errno 1] _ssl.c:504: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol

谢谢!

1 个答案:

答案 0 :(得分:3)

MAIL_PORT = 587
MAIL_USE_TLS = False
MAIL_USE_SSL = True

端口587用于显式TLS(STARTTLS命令),因此您可能必须使用MAIL_USE_TLS而不是MAIL_USE_SSL。 MAIL_USE_SSL用于隐式TLS,您可以在端口465上直接与TLS连接。