我正在尝试在YAML配置文件中设置SMTPHandler
。每当脚本失败时,我都希望日志记录器向启动脚本的人发送一封电子邮件(例如user_email
)。基于PEP 391,我试图通过在传入的字符串前面加上ext://
来引用该外部对象
# config.yaml
version: 1
handlers:
email:
class: logging.handlers.SMTPHandler
mailhost: ["smtp.office365.com", 587]
fromaddr: "email@place.com"
toaddrs: [ext://user_email]
subject: "TEST"
credentials: ["email@place.com", "password"]
secure: []
level: ERROR
loggers:
__main__:
level: INFO
handlers: [email]
# __main__.py
import yaml
import logging
import getpass
import logging.config
import logging.handlers
username = getpass.getuser()
user_email = f"{username}@place.com"
with open(r'./config.yaml') as cfg:
config = yaml.safe_load(cfg)
logging.config.dictConfig(config)
log = logging.getLogger(__name__)
try:
raise Exception()
except Exception:
log.exception('Unhandled Exception')
当我运行这个代码时,我得到一个smtplib.SMTPRecipientsRefused
错误,它说:
Traceback (most recent call last):
File "/Users/jessenestler/anaconda3/envs/facilityid_env/lib/python3.7/logging/handlers.py", line 1020, in emit
smtp.send_message(msg)
File "/Users/jessenestler/anaconda3/envs/facilityid_env/lib/python3.7/smtplib.py", line 967, in send_message
rcpt_options)
File "/Users/jessenestler/anaconda3/envs/facilityid_env/lib/python3.7/smtplib.py", line 881, in sendmail
raise SMTPRecipientsRefused(senderrs)
smtplib.SMTPRecipientsRefused: {'//user_email': (501, b'5.1.3 Invalid address')}
看起来//没有被正确剥离,但即使我在配置文件中写入toaddrs: [ext:user_email]
,仍然会得到相同的错误,但它会显示smtplib.SMTPRecipientsRefused: {'user_email': (501, b'5.1.3 Invalid address')}
如何使YAML配置文件接受user_email
变量,以便成功发送电子邮件?我是python3.7版本的
目前没有回答
相关问题 更多 >
编程相关推荐