Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problem with python logging.handlers.SMTPHandler, 'credentials' not recognized as attribute of SMTPHandler

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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'))

The print statements and traceback I’m getting is:

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'

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!

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading