Skateboss3
Skateboss3

Reputation: 31

smtplib bug? starttls/quit fails when called the second time

Take this code

import smtplib
s = smtplib.SMTP()
s.set_debuglevel(1)

print "-------------------------------------------------------------------------"
s.connect("smtp.gmail.com",587)
s.starttls()
s.login("USERNAME","PASSWORD")
s.quit()

print "========================================================================="

s.connect("smtp.gmail.com",587)
s.starttls()
s.login("USERNAME","PASSWORD")
s.quit()
print "-------------------------------------------------------------------------"

first time it connects, it starts just fine.... second time it produces an exception about TLS as seen here...

Traceback (most recent call last):
  File "mtest.py", line 12, in <module>
    s.starttls()
  File "/usr/lib/python2.7/smtplib.py", line 635, in starttls
    raise SMTPException("STARTTLS extension not supported by server.")
smtplib.SMTPException: STARTTLS extension not supported by server.

Am I missing something stupid or is there a bug?

P.s. I wrote this test as I was getting the same problem in my other 'threaded' script, so wanted to make sure that it was nothing to do with threading.

Upvotes: 2

Views: 538

Answers (1)

msw
msw

Reputation: 43487

I'm guessing that after the s.quit() the connection s is dead and cannot be used for anything else. If so, you'd need another s = smtplib.SMTP() for the second mail transfer.

The quit() documentation seems to suggest this is so:

Terminate the SMTP session and close the connection.

Upvotes: 3

Related Questions