smtplib, gmail, quit vs. close and SSL3_GET_RECORD:wrong version number

Roger crosseyedpenguin at digitalrockart.phxcoxmail.com
Sun Aug 5 16:01:40 EDT 2007


I am having a problem sending email through smtp.gmail.com using 
smtplib.  Everything works and the mail is sent and received, except 
quit.  The following shows the problem (without bothering to login or do 
the sendmail):

>>>> import smtplib
>>>> server = smtplib.SMTP('smtp.gmail.com',25)
>>>> server.ehlo()
> (250, 'mx.google.com at your service, [68.98.218.211]\nSIZE 28311552\n8BITMIME\nSTARTTLS\nENHANCEDSTATUSCODES')
>>>> server.starttls()
> (220, '2.0.0 Ready to start TLS')
>>>> server.ehlo()
> (250, 'mx.google.com at your service, [68.98.218.211]\nSIZE 28311552\n8BITMIME\nAUTH LOGIN PLAIN\nENHANCEDSTATUSCODES')
>>>> server.noop()
> (250, '2.0.0 OK')
>>>> server.quit()
> 
> Traceback (most recent call last):
>   File "<pyshell#6>", line 1, in <module>
>     server.quit()
>   File "c:\Python25\lib\smtplib.py", line 716, in quit
>     self.docmd("quit")
>   File "c:\Python25\lib\smtplib.py", line 378, in docmd
>     return self.getreply()
>   File "c:\Python25\lib\smtplib.py", line 352, in getreply
>     line = self.file.readline()
>   File "c:\Python25\lib\smtplib.py", line 160, in readline
>     chr = self.sslobj.read(1)
> sslerror: (1, 'error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number')
>>>> 

I have not had this problem with other mail servers (but only tried 
two).  A solution for gmail seems to be to replace the server.quit() 
with server.close().  The difference between the two commands is quit() 
sends a 'QUIT' string before calling close().  Because close() is not 
included in the smtplib docs, it does not appear to be the right solution.

What is the correct way to terminate a gmail session?

Roger



More information about the Python-list mailing list