How can I fix a ‘socket.gaierror: [Errno 11001] getaddrinfo failed’ when trying to use an SMTPHandler in Python ‘logging’?

I am currently trying to log using the SMTPHandler class from Python’s logging module. The logger is instantiated just fine, but fails when calling _socket.getaddrinfo even though it is being passed parameters which seem to work elsewhere. Below is the error message I get:

--- Logging error ---
Traceback (most recent call last):
  File "C:UsersUserAppDataLocalProgramsPythonPython311Liblogginghandlers.py", line 1075, in emit
    smtp = smtplib.SMTP(self.mailhost, port, timeout=self.timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:UsersUserAppDataLocalProgramsPythonPython311Libsmtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
                  ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:UsersUserAppDataLocalProgramsPythonPython311Libsmtplib.py", line 341, in connect
    self.sock = self._get_socket(host, port, self.timeout)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:UsersUserAppDataLocalProgramsPythonPython311Libsmtplib.py", line 312, in _get_socket
    return socket.create_connection((host, port), timeout,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:UsersUserAppDataLocalProgramsPythonPython311Libsocket.py", line 827, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:UsersUserAppDataLocalProgramsPythonPython311Libsocket.py", line 962, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno 11001] getaddrinfo failed
Call stack:
  File "<string>", line 1, in <module>
  File "C:UsersUserAppDataLocalProgramsPythonPython311Libidlelibrun.py", line 165, in main
    ret = method(*args, **kwargs)
  File "C:UsersUserAppDataLocalProgramsPythonPython311Libidlelibrun.py", line 579, in runcode
    exec(code, self.locals)
  File "C:UsersUserDesktoptest_logtest_log.py", line 34, in <module>
    main()
  File "C:UsersUserDesktoptest_logtest_log.py", line 28, in main
    log.critical('Testing email logging')
Message: 'Testing email logging'
Arguments: ()

I’m configuring the email handler and attempting to log as below.

Handler Config:

"emailhand": {
            "class": "logging.handlers.SMTPHandler",
            "level": "CRITICAL",
            "formatter": "a_formatter",
            "mailhost": "('send.smtp.com', 25)",
            "credentials": "('[email protected]', 'password')",
            "fromaddr": "[email protected]",
            "toaddrs": "[email protected]",
            "subject": "Test Log",
            "secure": "()",
            "timeout": 10.0
        }

Python Module:

import os
import json
import logging
import logging.config
import logging.handlers

def init_logging():

    logfile = r'C:UsersUserDesktoptest_logtest_log.log'
    
    with open(r'C:UsersUserDesktoptest_logfile-datalogging.json', 'r') as f:
        LOGGING_CONFIG = json.load(f)
    
    logging.config.dictConfig(LOGGING_CONFIG)


def main():
    init_logging()
    log = logging.getLogger('test_log')

    log.info('Some info from the main function')
    log.critical('Testing email logging')


if __name__ == "__main__":
    main()

Currently I’m using TLS when connecting to the email server, but I’ve also tried with SSL and port 465 and this hasn’t worked either. I’ve been able to successfully send emails using the smtp library with both TLS and SSL outside of this module by using smtplib.SMTP. I’ve attempted to alter the timeout value (anywhere from 1 to 10000) and this seems to have made no difference. I’ve also attempted to alter the emit function to use SMTP_SSL instead of SMTP and this didn’t work. I’ve run socket.getaddrinfo with the same host and port as above and received the following (I’ve replaced the IP numbers with ‘#’):

[(<AddressFamily.AF_INET: 2>, 0, 0, '', ('###.##.###.##', 25)), (<AddressFamily.AF_INET: 2>, 0, 0, '', ('###.##.###.##', 25))]

As far as I can tell the above means that socket is working fine. What could be causing this issue?

New contributor

RealSwellThanks is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật