use*_*661 2 python logging smtp
我用Verizon和Gmail试过这个.两台服务器都拒绝认证 Gmail通过电子邮件通知我它拒绝登录尝试,因为连接没有使用"现代安全".
我想知道如何使用这个日志记录处理程序的现代安全性.
logging.handlers.SMTPHandler(mailhost=('', 25),
fromaddr='',
toaddrs='',
subject='',
credentials=('username','password'),
secure=())
Run Code Online (Sandbox Code Playgroud)
对于回到此处的任何人,以下是我使用Gmail的SMTPHandler的方法:
eh = SMTPHandler(mailhost=('smtp.gmail.com', 587),
fromaddr=from_addr,
toaddrs=to_addrs,
subject=subject,
credentials=(username, password),
secure=())
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
我切换到587端口,谷歌成功通过身份验证.消息已发送.
Gmail问题:
Verizon问题:
smtp.verizon.net就是这样一个例子.SMTPHandler默认情况下不支持SMTPS.您可以monkeypatch功能.解:
然后设置它
logging.handlers.SMTPHandler.emit = emit
Run Code Online (Sandbox Code Playgroud)
默认的/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)
Run Code Online (Sandbox Code Playgroud)
编辑的发射功能
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)
Run Code Online (Sandbox Code Playgroud)