--- smtplib.py.orig 2007-10-30 10:30:16.000000000 -0700 +++ smtplib.py 2007-10-30 10:38:00.000000000 -0700 @@ -505,6 +505,23 @@ # some useful methods + def ehlo_or_helo_if_needed(self): + """Call self.ehlo() and/or self.helo() if needed. + + If there has been no previous EHLO or HELO command this session, this + method tries ESMTP EHLO first. + + This method may raise the following exceptions: + + SMTPHeloError The server didn't reply properly to + the helo greeting. + """ + if self.helo_resp is None and self.ehlo_resp is None: + if not (200 <= self.ehlo()[0] <= 299): + (code, resp) = self.helo() + if not (200 <= code <= 299): + raise SMTPHeloError(code, resp) + def login(self, user, password): """Log in on an SMTP server that requires authentication. @@ -540,11 +557,7 @@ AUTH_CRAM_MD5 = "CRAM-MD5" AUTH_LOGIN = "LOGIN" - if self.helo_resp is None and self.ehlo_resp is None: - if not (200 <= self.ehlo()[0] <= 299): - (code, resp) = self.helo() - if not (200 <= code <= 299): - raise SMTPHeloError(code, resp) + self.ehlo_or_helo_if_needed() if not self.has_extn("auth"): raise SMTPException("SMTP AUTH extension not supported by server.") @@ -590,12 +603,23 @@ def starttls(self, keyfile = None, certfile = None): """Puts the connection to the SMTP server into TLS mode. + If there has been no previous EHLO or HELO command this session, this + method tries ESMTP EHLO first. + If the server supports TLS, this will encrypt the rest of the SMTP session. If you provide the keyfile and certfile parameters, the identity of the SMTP server and client can be checked. This, however, depends on whether the socket module really checks the certificates. + + This method may raise the following exceptions: + + SMTPHeloError The server didn't reply properly to + the helo greeting. """ + self.ehlo_or_helo_if_needed() + if not self.has_extn("starttls"): + raise SMTPException("STARTTLS extension not supported by server.") (resp, reply) = self.docmd("STARTTLS") if resp == 220: sslobj = socket.ssl(self.sock, keyfile, certfile) @@ -660,11 +684,7 @@ empty dictionary. """ - if self.helo_resp is None and self.ehlo_resp is None: - if not (200 <= self.ehlo()[0] <= 299): - (code,resp) = self.helo() - if not (200 <= code <= 299): - raise SMTPHeloError(code, resp) + self.ehlo_or_helo_if_needed() esmtp_opts = [] if self.does_esmtp: # Hmmm? what's this? -ddm