kefeizhou
kefeizhou

Reputation: 6552

Python Smtp SSL wrong version on linux

My code to send emails via msft outlook.com works on windows but not on my linux box. Any idea how to fix this?

import smtplib
smtp = smtplib.SMTP('smtp.live.com', port=587)
smtp.starttls()
smtp.login(username, password)

SMTPServerDisconnected: Connection unexpectedly closed: 
[Errno 1] _ssl.c:1359: error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number

EDIT: more details: gentoo linux with python 2.7.3, openssl 0.9.8x and 1.0.1c

Upvotes: 2

Views: 3350

Answers (1)

kirelagin
kirelagin

Reputation: 13616

I bet the problem is on the other side of the wire. Hello, Microsoft!

I've tried logging in a number of times, and you won't believe me, but some servers will let me in, while others won't. Try doing smtp.ehlo() in order to find the server's hostname (by the way, you must issue EHLO at the beginning of your session, and immediately after STARTTLS).

All their servers have names BLU0-SMTP<somenumber>phx.gbl. Believe me or not, but servers with two digits in their name are OK, but those with three digits are not.

In [52]: s = smtplib.SMTP('smtp.live.com', port=587)

In [53]: s.ehlo()
Out[53]:
(250,
 'BLU0-SMTP17.phx.gbl Hello [188.134.8.114]\nTURN\nSIZE 41943040\nETRN\nPIPELINING\nDSN\nENHANCEDSTATUSCODES\n8bitmime\nBINARYMIME\nCHUNKING\nVRFY\nTLS\nSTARTTLS\nOK')

In [54]: s.starttls()
Out[54]: (220, '2.0.0 SMTP server ready')

In [55]: s.ehlo()
Out[55]:
(250,
 'BLU0-SMTP17.phx.gbl Hello [188.134.8.114]\nTURN\nSIZE 41943040\nETRN\nPIPELINING\nDSN\nENHANCEDSTATUSCODES\n8bitmime\nBINARYMIME\nCHUNKING\nVRFY\nAUTH LOGIN PLAIN\nOK')

In [56]: s.login(login, password)
Out[56]: (235, '2.7.0 Authentication succeeded')
In [42]: s = smtplib.SMTP('smtp.live.com', port=587)

In [43]: s.ehlo()
Out[43]:
(250,
 'BLU0-SMTP116.phx.gbl Hello [188.134.8.114]\nTURN\nSIZE 41943040\nETRN\nPIPELINING\nDSN\nENHANCEDSTATUSCODES\n8bitmime\nBINARYMIME\nCHUNKING\nVRFY\nTLS\nSTARTTLS\nOK')

In [44]: s.starttls()
Out[44]: (220, '2.0.0 SMTP server ready')

In [45]: s.ehlo()
Out[45]:
(250,
 'BLU0-SMTP116.phx.gbl Hello [188.134.8.114]\nTURN\nSIZE 41943040\nETRN\nPIPELINING\nDSN\nENHANCEDSTATUSCODES\n8bitmime\nBINARYMIME\nCHUNKING\nVRFY\nAUTH LOGIN PLAIN\nOK')

In [46]: s.login(login, password)
---------------------------------------------------------------------------
SMTPServerDisconnected                    Traceback (most recent call last)

Update: Hm, seems that it is a known issue with 1.0.1c.

Upvotes: 2

Related Questions