- Issue #11289: smtp.SMTP class becomes a context manager so it can be used

in a `with` statement.  Contributed by Giampaolo Rodola.
This commit is contained in:
Barry Warsaw 2011-03-15 15:04:44 -04:00
parent 5eb3591a41
commit 1f5c958721
4 changed files with 66 additions and 2 deletions

View file

@ -269,6 +269,19 @@ class SMTP:
pass
self.local_hostname = '[%s]' % addr
def __enter__(self):
return self
def __exit__(self, *args):
try:
code, message = self.docmd("QUIT")
if code != 221:
raise SMTPResponseException(code, message)
except SMTPServerDisconnected:
pass
finally:
self.close()
def set_debuglevel(self, debuglevel):
"""Set the debug output level.