![](/img/trans.png)
[英]logging.handlers.SMTPHandler raises smtplib.SMTPAuthenticationError
[英]Problem with python logging.handlers.SMTPHandler, 'credentials' not recognized as attribute of SMTPHandler
我正在嘗試在我的 python 應用程序中設置嚴重錯誤的 email 日志記錄。 我在嘗試初始化 SMTPHandler 時一直遇到錯誤:
AttributeError: 'SMTPHandler' object 沒有屬性 'credentials'
我正在使用 Python 3.10。 我在出現錯誤的地方創建了一個程序組件。
import logging
from logging.handlers import SMTPHandler
mail_handler = SMTPHandler(
mailhost='my.hosting.com',
fromaddr='admin@myapp.com',
toaddrs=['admin@myapp.com'],
subject='Application Error',
credentials=('admin@myapp.com', 'mypassword'),
secure=()
)
print(mail_handler.mailhost)
print(mail_handler.fromaddr)
print(mail_handler.toaddrs)
print(mail_handler.subject)
print(mail_handler.secure)
print(mail_handler.timeout)
print(mail_handler.credentials)
mail_handler.setLevel(logging.ERROR)
mail_handler.setFormatter(logging.Formatter('[%(asctime)s] %(levelname)s in %(module)s: %(message)s'))
我得到的打印語句和回溯是:
my.hosting.com
admin@myapp.com
['admin@myapp.com']
Application Error
()
5.0
Traceback (most recent call last):
File "C:\Users\user\Documents\myapp\test.py", line 31, in <module>
print(mail_handler.credentials)
AttributeError: 'SMTPHandler' object has no attribute 'credentials'
當我使用以下代碼片段檢查 SMTPHandler 的 init 語句以確保我沒有訪問非常舊的版本時(我認為憑據是在 2.6 中添加的):
import inspect
signature = inspect.signature(SMTPHandler.__init__).parameters
for name, parameter in signature.items():
print(name, parameter.default, parameter.annotation, parameter.kind)`
我得到:
self <class 'inspect._empty'> <class 'inspect._empty'> POSITIONAL_OR_KEYWORD
mailhost <class 'inspect._empty'> <class 'inspect._empty'> POSITIONAL_OR_KEYWORD
fromaddr <class 'inspect._empty'> <class 'inspect._empty'> POSITIONAL_OR_KEYWORD
toaddrs <class 'inspect._empty'> <class 'inspect._empty'> POSITIONAL_OR_KEYWORD
subject <class 'inspect._empty'> <class 'inspect._empty'> POSITIONAL_OR_KEYWORD
credentials None <class 'inspect._empty'> POSITIONAL_OR_KEYWORD
secure None <class 'inspect._empty'> POSITIONAL_OR_KEYWORD
timeout 5.0 <class 'inspect._empty'> POSITIONAL_OR_KEYWORD
所以'credentials'在初始化語句中。
有人在我的代碼中看到了一些愚蠢的東西或遇到了這個問題嗎?
非常感謝!
您在計算機上擁有所有標准模塊的完整源代碼。 我只是快速瀏覽了一下,雖然SMTPHandler
接受一個credentials
參數,但它將該參數存儲在self.username
和self.password
中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.