Backend Development 3 min read

Configuring Python logging.handlers.SMTPHandler for Email Logging with TLS/SSL

This article explains how to configure Python's logging.handlers.SMTPHandler to send log messages via email, addresses common authentication timeout issues with QQ corporate mail by using TLS/SSL connections, and provides a complete logging.conf example with a test script.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Configuring Python logging.handlers.SMTPHandler for Email Logging with TLS/SSL

The author encountered login‑timeout errors when trying to send log records via email using Python's logging.handlers.SMTPHandler with a QQ corporate mailbox, prompting an investigation into the underlying cause.

By examining the source of the logging module, it was discovered that the default logging handler expects a TLS connection, while QQ (and Gmail) require an SSL connection, leading to the authentication failure.

The solution is to configure the SMTP handler to use the appropriate security protocol and correct server settings. A full logging.conf file is provided, showing how to define loggers, handlers, formatters, and the SMTP handler with TLS/SSL parameters.

Finally, a short Python script demonstrates loading the configuration and sending a test log email.

# logging.conf complete configuration
[loggers]
keys=root,test

[handlers]
keys=consoleHandler,fileHandler,testHandler

[formatters]
keys=simpleFormatter

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s - [%(filename)s:%(lineno)s]
datefmt=

[logger_root]
level=INFO
handlers=consoleHandler,fileHandler

[logger_test]
level=INFO
handlers=testHandler
qualname=test
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=INFO
formatter=simpleFormatter
args=(sys.stdout,)

[handler_fileHandler]
class=FileHandler
level=INFO
formatter=simpleFormatter
args=('log/spider_db.log', 'a')

[handler_testHandler]
class=handlers.SMTPHandler
level=INFO
formatter=simpleFormatter
args=(('smtp.163.com',25), '[email protected]', ['[email protected]','[email protected]'], 'Test SMTPHandler', ('username', 'password'))

# Email test example
import logging
import logging.config

logging.config.fileConfig("logging.conf")
logger = logging.getLogger('test')
logger.info('hello body ~')
BackendpythonLoggingTLSemailSMTP
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.