Sending email without keyfile (only certfile) using Python smtplib

前端 未结 2 852
萌比男神i
萌比男神i 2021-01-14 16:31

Trying to send email with a certificate file using the following script:

import smtplib

client = smtplib.SMTP(myhost, myport)
client.ehlo()
client.starttls(         


        
2条回答
  •  终归单人心
    2021-01-14 17:09

    Here's a monkey-patch taken from this page:

    class SMTPExt(smtplib.SMTP):
        """
        This class extends smtplib.SMTP and overrides the starttls method
        allowing extra parameters and forwarding them to ssl.wrap_socket.
        """
    
        def starttls(self, keyfile=None, certfile=None, **kwargs):
            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:
                self.sock = ssl.wrap_socket(self.sock, keyfile, certfile, **kwargs)
                self.file = SSLFakeFile(self.sock)
                # RFC 3207:
                # The client MUST discard any knowledge obtained from
                # the server, such as the list of SMTP service extensions,
                # which was not obtained from the TLS negotiation itself.
                self.helo_resp = None
                self.ehlo_resp = None
                self.esmtp_features = {}
                self.does_esmtp = 0
            return (resp, reply)
    

    Using the root cert from requests

提交回复
热议问题