Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python logging.handlers.SMTPHandler引发smtplib.SMTPAuthenticationError_Python_Logging_Smtp - Fatal编程技术网

Python logging.handlers.SMTPHandler引发smtplib.SMTPAuthenticationError

Python logging.handlers.SMTPHandler引发smtplib.SMTPAuthenticationError,python,logging,smtp,Python,Logging,Smtp,我用Verizon和Gmail试过了。两台服务器都拒绝了身份验证。Gmail给我发电子邮件说它拒绝了登录尝试,因为连接没有使用“现代安全性”。 我想知道如何使用这个日志处理程序的现代安全性 logging.handlers.SMTPHandler(mailhost=('', 25), fromaddr='', toaddrs='',

我用Verizon和Gmail试过了。两台服务器都拒绝了身份验证。Gmail给我发电子邮件说它拒绝了登录尝试,因为连接没有使用“现代安全性”。
我想知道如何使用这个日志处理程序的现代安全性

logging.handlers.SMTPHandler(mailhost=('', 25),
                             fromaddr='',
                             toaddrs='',
                             subject='',
                             credentials=('username','password'),
                             secure=())
Gmail问题:

  • 这里没有提到。查看关于Gmail应用程序认证的其他答案
Verizon问题:

  • 某些邮件提交服务器可能只接受端口465上的SMTPS。有关详细说明,请参阅。Verizon邮件提交服务器smtp.Verizon.net就是这样一个例子
  • SMTPHandler
    默认情况下不支持SMTPS。您可以对该功能进行monkeypatch
解决方案:

  • 这将强制任何服务器使用SMTPS
  • 更彻底的修复方法是使用一个标志来编辑类,以启用SMTPS

  • 从下面将已编辑的发射函数粘贴到相关文件中
  • 那就定下来

    logging.handlers.SMTPHandler.emit = emit
    
  • 默认的/logging/handlers.py
    logging.handlers.SMTPHandler.emit
    函数

    # Class SMTPHandler...
    def emit(self, record):
        """
        Emit a record.
    
        Format the record and send it to the specified addressees.
        """
        try:
            import smtplib
            from email.utils import formatdate
            port = self.mailport
            if not port:
                port = smtplib.SMTP_PORT
            smtp = smtplib.SMTP(self.mailhost, port, timeout=self._timeout)
            msg = self.format(record)
            msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\nDate: %s\r\n\r\n%s" % (
                            self.fromaddr,
                            ",".join(self.toaddrs),
                            self.getSubject(record),
                            formatdate(), msg)
            if self.username:
                if self.secure is not None:
                    smtp.ehlo()
                    smtp.starttls(*self.secure)
                    smtp.ehlo()
                smtp.login(self.username, self.password)
            smtp.sendmail(self.fromaddr, self.toaddrs, msg)
            smtp.quit()
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            self.handleError(record)
    
    def emit(self, record):
        """
        Overwrite the logging.handlers.SMTPHandler.emit function with SMTP_SSL.
        Emit a record.
        Format the record and send it to the specified addressees.
        """
        try:
            import smtplib
            from email.utils import formatdate
            port = self.mailport
            if not port:
                port = smtplib.SMTP_PORT
            smtp = smtplib.SMTP_SSL(self.mailhost, port, timeout=self._timeout)
            msg = self.format(record)
            msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\nDate: %s\r\n\r\n%s" % (self.fromaddr, ", ".join(self.toaddrs), self.getSubject(record), formatdate(), msg)
            if self.username:
                smtp.ehlo()
                smtp.login(self.username, self.password)
            smtp.sendmail(self.fromaddr, self.toaddrs, msg)
            smtp.quit()
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            self.handleError(record)
    

    编辑的发射功能

    # Class SMTPHandler...
    def emit(self, record):
        """
        Emit a record.
    
        Format the record and send it to the specified addressees.
        """
        try:
            import smtplib
            from email.utils import formatdate
            port = self.mailport
            if not port:
                port = smtplib.SMTP_PORT
            smtp = smtplib.SMTP(self.mailhost, port, timeout=self._timeout)
            msg = self.format(record)
            msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\nDate: %s\r\n\r\n%s" % (
                            self.fromaddr,
                            ",".join(self.toaddrs),
                            self.getSubject(record),
                            formatdate(), msg)
            if self.username:
                if self.secure is not None:
                    smtp.ehlo()
                    smtp.starttls(*self.secure)
                    smtp.ehlo()
                smtp.login(self.username, self.password)
            smtp.sendmail(self.fromaddr, self.toaddrs, msg)
            smtp.quit()
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            self.handleError(record)
    
    def emit(self, record):
        """
        Overwrite the logging.handlers.SMTPHandler.emit function with SMTP_SSL.
        Emit a record.
        Format the record and send it to the specified addressees.
        """
        try:
            import smtplib
            from email.utils import formatdate
            port = self.mailport
            if not port:
                port = smtplib.SMTP_PORT
            smtp = smtplib.SMTP_SSL(self.mailhost, port, timeout=self._timeout)
            msg = self.format(record)
            msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\nDate: %s\r\n\r\n%s" % (self.fromaddr, ", ".join(self.toaddrs), self.getSubject(record), formatdate(), msg)
            if self.username:
                smtp.ehlo()
                smtp.login(self.username, self.password)
            smtp.sendmail(self.fromaddr, self.toaddrs, msg)
            smtp.quit()
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            self.handleError(record)
    

    对于回到这里的任何人,以下是我如何让SMTPHandler与Gmail一起工作的:

    eh = SMTPHandler(mailhost=('smtp.gmail.com', 587),
                    fromaddr=from_addr,
                    toaddrs=to_addrs,
                    subject=subject,
                    credentials=(username, password),
                    secure=())
    
    在我的示例中,
    to_addrs
    变量是一个字符串。我不确定它是否可以是一个数组,或者应该是一个空格或逗号分隔的字符串。
    username
    变量包括域,如下所示:
    foo@gmail.com

    大多数导游都说使用465端口,如果你好奇的话,这里有一些建议。但是,当我尝试使用端口465时,出现SMTP超时错误:

    Traceback (most recent call last):
    File "/usr/local/lib/python3.5/smtplib.py", line 386, in getreply
      line = self.file.readline(_MAXLINE + 1)
    File "/usr/local/lib/python3.5/socket.py", line 571, in readinto
      return self._sock.recv_into(b)
    socket.timeout: timed out
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
    File "/usr/local/lib/python3.5/logging/handlers.py", line 972, in emit
      smtp = smtplib.SMTP(self.mailhost, port, timeout=self.timeout)
    File "/usr/local/lib/python3.5/smtplib.py", line 251, in __init__
      (code, msg) = self.connect(host, port)
    File "/usr/local/lib/python3.5/smtplib.py", line 337, in connect
      (code, msg) = self.getreply()
    File "/usr/local/lib/python3.5/smtplib.py", line 390, in getreply
      + str(e))
    smtplib.SMTPServerDisconnected: Connection unexpectedly closed: timed out
    

    我切换到587端口,谷歌成功地进行了身份验证。邮件已发送。

    我需要添加eh。_timeout=30以延长默认超时5秒,这样才能可靠工作。端口465(SSL)的问题是始终使用
    smtplib.SMTP()
    可以选择使用StartTLS,而不是
    smtplib.SMTP\u SSL()