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 an
(self, keyfile=None, certfile=None, context=None)
| 750 | raise last_exception |
| 751 | |
| 752 | def starttls(self, keyfile=None, certfile=None, context=None): |
| 753 | """Puts the connection to the SMTP server into TLS mode. |
| 754 | |
| 755 | If there has been no previous EHLO or HELO command this session, this |
| 756 | method tries ESMTP EHLO first. |
| 757 | |
| 758 | If the server supports TLS, this will encrypt the rest of the SMTP |
| 759 | session. If you provide the keyfile and certfile parameters, |
| 760 | the identity of the SMTP server and client can be checked. This, |
| 761 | however, depends on whether the socket module really checks the |
| 762 | certificates. |
| 763 | |
| 764 | This method may raise the following exceptions: |
| 765 | |
| 766 | SMTPHeloError The server didn't reply properly to |
| 767 | the helo greeting. |
| 768 | """ |
| 769 | self.ehlo_or_helo_if_needed() |
| 770 | if not self.has_extn("starttls"): |
| 771 | raise SMTPNotSupportedError( |
| 772 | "STARTTLS extension not supported by server.") |
| 773 | (resp, reply) = self.docmd("STARTTLS") |
| 774 | if resp == 220: |
| 775 | if not _have_ssl: |
| 776 | raise RuntimeError("No SSL support included in this Python") |
| 777 | if context is not None and keyfile is not None: |
| 778 | raise ValueError("context and keyfile arguments are mutually " |
| 779 | "exclusive") |
| 780 | if context is not None and certfile is not None: |
| 781 | raise ValueError("context and certfile arguments are mutually " |
| 782 | "exclusive") |
| 783 | if keyfile is not None or certfile is not None: |
| 784 | import warnings |
| 785 | warnings.warn("keyfile and certfile are deprecated, use a " |
| 786 | "custom context instead", DeprecationWarning, 2) |
| 787 | if context is None: |
| 788 | context = ssl._create_stdlib_context(certfile=certfile, |
| 789 | keyfile=keyfile) |
| 790 | self.sock = context.wrap_socket(self.sock, |
| 791 | server_hostname=self._host) |
| 792 | self.file = None |
| 793 | # RFC 3207: |
| 794 | # The client MUST discard any knowledge obtained from |
| 795 | # the server, such as the list of SMTP service extensions, |
| 796 | # which was not obtained from the TLS negotiation itself. |
| 797 | self.helo_resp = None |
| 798 | self.ehlo_resp = None |
| 799 | self.esmtp_features = {} |
| 800 | self.does_esmtp = False |
| 801 | else: |
| 802 | # RFC 3207: |
| 803 | # 501 Syntax error (no parameters allowed) |
| 804 | # 454 TLS not available due to temporary reason |
| 805 | raise SMTPResponseException(resp, reply) |
| 806 | return (resp, reply) |
| 807 | |
| 808 | def sendmail(self, from_addr, to_addrs, msg, mail_options=(), |
| 809 | rcpt_options=()): |
no test coverage detected