logging.handlers.SMTPHandler 提高 smtplib.SMTPAuthenticationError
logging.handlers.SMTPHandler raises smtplib.SMTPAuthenticationError
我在 Verizon 和 Gmail 上试过了。两台服务器均拒绝身份验证。 Gmail 给我发邮件说它拒绝登录尝试,因为连接没有使用 "modern security".
我想知道如何通过此日志记录处理程序使用现代安全性。
logging.handlers.SMTPHandler(mailhost=('', 25),
fromaddr='',
toaddrs='',
subject='',
credentials=('username','password'),
secure=())
Gmail 问题:
- 此处未提及。查看有关 Gmail 应用程序身份验证的其他答案。
威瑞森问题:
- 一些邮件提交服务器可能只接受端口 465 上的 SMTPS。详见 What is the difference between ports 465 and 587?。 Verizon 邮件提交服务器
smtp.verizon.net
就是这样的一个例子。
SMTPHandler
默认不支持SMTPS。您可以对功能进行猴子修补。
解决方案:
- 这会强制任何服务器使用 SMTPS。
- 更彻底的修复方法是使用标志编辑 class 以启用 SMTPS。
- 将下面编辑的发射函数粘贴到相关文件中。
然后设置
logging.handlers.SMTPHandler.emit = emit
默认/logging/handlers.pylogging.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)
修改后的emit函数
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
变量在我的示例中是一个字符串。我不确定它是否可以是数组或者应该是 space 或逗号分隔的字符串。 username
变量包含域,如下所示:foo@gmail.com
.
大多数指南说使用端口 465,如果您好奇,这里是 difference。但是,当我尝试使用端口 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端口,Google认证成功。消息已发送。
我在 Verizon 和 Gmail 上试过了。两台服务器均拒绝身份验证。 Gmail 给我发邮件说它拒绝登录尝试,因为连接没有使用 "modern security".
我想知道如何通过此日志记录处理程序使用现代安全性。
logging.handlers.SMTPHandler(mailhost=('', 25),
fromaddr='',
toaddrs='',
subject='',
credentials=('username','password'),
secure=())
Gmail 问题:
- 此处未提及。查看有关 Gmail 应用程序身份验证的其他答案。
威瑞森问题:
- 一些邮件提交服务器可能只接受端口 465 上的 SMTPS。详见 What is the difference between ports 465 and 587?。 Verizon 邮件提交服务器
smtp.verizon.net
就是这样的一个例子。 SMTPHandler
默认不支持SMTPS。您可以对功能进行猴子修补。
解决方案:
- 这会强制任何服务器使用 SMTPS。
- 更彻底的修复方法是使用标志编辑 class 以启用 SMTPS。
- 将下面编辑的发射函数粘贴到相关文件中。
然后设置
logging.handlers.SMTPHandler.emit = emit
默认/logging/handlers.pylogging.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)
修改后的emit函数
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
变量在我的示例中是一个字符串。我不确定它是否可以是数组或者应该是 space 或逗号分隔的字符串。 username
变量包含域,如下所示:foo@gmail.com
.
大多数指南说使用端口 465,如果您好奇,这里是 difference。但是,当我尝试使用端口 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端口,Google认证成功。消息已发送。