我正在使用SMTPHandler在我的python脚本中记录捕获的异常,该脚本向后端请求新数据。
这是我初始化SMTPHandler的方式:
# write errors to email
error_mail_subject = "ERROR: Script error in %s" % sys.argv[0]
error_mail_handler = logging.handlers.SMTPHandler(mailhost=("smtp.gmail.com", 587),
fromaddr="test@gmail.com",
toaddrs="test@gmail.com",
subject=error_mail_subject)
error_mail_handler.setLevel(logging.ERROR)
error_mail_handler.setFormatter(debug_format)
# Build the logger
logger = logging.getLogger()
logger.addHandler(error_mail_handler)
但是不幸的是,在处理脚本异常期间出现以下错误
smtplib.SMTPSenderRefused: (530, b'5.7.0 Must issue a STARTTLS command first. w15sm3562054qta.16 - gsmtp', 'test@gmail.com')
脚本调用时:
logger.exception(e)
答案 0 :(得分:1)
这已成功发送了来自我的gmail的电子邮件,并经过了立即测试:
import logging.handlers
error_mail_subject = "hello, it's me"
error_mail_handler = logging.handlers.SMTPHandler(mailhost=("smtp.gmail.com", 587),
fromaddr="email",
toaddrs="email",
subject=error_mail_subject,
credentials=('email', 'password'),
secure=())
error_mail_handler.setLevel(logging.ERROR)
logger = logging.getLogger()
logger.addHandler(error_mail_handler)
logger.exception(Exception("hey"))
我还必须登录gmail,然后转到此处以允许访问不太安全的应用:https://www.google.com/settings/security/lesssecureapps