Problem with python logging.handlers.SMTPHandler, 'credentials' not recognized as attribute of SMTPHandler
Question:
I’m trying to set up email logging of critical errors in my python application. I keep running into an error trying to initialize the SMTPHandler:
AttributeError: ‘SMTPHandler’ object has no attribute ‘credentials’
I’m using Python 3.10. I carved out a component of the program where I’m getting the error.
import logging
from logging.handlers import SMTPHandler
mail_handler = SMTPHandler(
mailhost='my.hosting.com',
fromaddr='[email protected]',
toaddrs=['[email protected]'],
subject='Application Error',
credentials=('[email protected]', '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'))
The print statements and traceback I’m getting is:
my.hosting.com
[email protected]
['[email protected]']
Application Error
()
5.0
Traceback (most recent call last):
File "C:UsersuserDocumentsmyapptest.py", line 31, in <module>
print(mail_handler.credentials)
AttributeError: 'SMTPHandler' object has no attribute 'credentials'
When I check the init statement for SMTPHandler using the following snippet to make sure I’m not accessing a very old version (I think credentials was added in 2.6):
import inspect
signature = inspect.signature(SMTPHandler.__init__).parameters
for name, parameter in signature.items():
print(name, parameter.default, parameter.annotation, parameter.kind)`
I get:
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
So ‘credentials’ is in the initialization statement.
Anyone see something stupid in my code or run into this problem?
Thanks so much!
Answers:
You have the full source code for all of the standard modules on your computer. I just took a quick look, and although the SMTPHandler
accepts a credentials
argument, it stores that argument in self.username
and self.password
.
I’m trying to set up email logging of critical errors in my python application. I keep running into an error trying to initialize the SMTPHandler:
AttributeError: ‘SMTPHandler’ object has no attribute ‘credentials’
I’m using Python 3.10. I carved out a component of the program where I’m getting the error.
import logging
from logging.handlers import SMTPHandler
mail_handler = SMTPHandler(
mailhost='my.hosting.com',
fromaddr='[email protected]',
toaddrs=['[email protected]'],
subject='Application Error',
credentials=('[email protected]', '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'))
The print statements and traceback I’m getting is:
my.hosting.com
[email protected]
['[email protected]']
Application Error
()
5.0
Traceback (most recent call last):
File "C:UsersuserDocumentsmyapptest.py", line 31, in <module>
print(mail_handler.credentials)
AttributeError: 'SMTPHandler' object has no attribute 'credentials'
When I check the init statement for SMTPHandler using the following snippet to make sure I’m not accessing a very old version (I think credentials was added in 2.6):
import inspect
signature = inspect.signature(SMTPHandler.__init__).parameters
for name, parameter in signature.items():
print(name, parameter.default, parameter.annotation, parameter.kind)`
I get:
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
So ‘credentials’ is in the initialization statement.
Anyone see something stupid in my code or run into this problem?
Thanks so much!
You have the full source code for all of the standard modules on your computer. I just took a quick look, and although the SMTPHandler
accepts a credentials
argument, it stores that argument in self.username
and self.password
.