Skip to content

SMTPMailer not passing 'local_hostname' to base Python SMTP class #50

@roryodonnell

Description

@roryodonnell

I am experiencing an issue with the Google SMTP relay mailer. The Google SMTP service receives 127.0.0.1 and is refusing to connect, because the local_hostname is not set.

2023-07-20 17:03:06,261: WARNING/ForkPoolWorker-2] send:
[2023-07-20 17:03:06,262: WARNING/ForkPoolWorker-2] 'ehlo [127.0.0.1]\r\n'
[2023-07-20 17:03:06,317: WARNING/ForkPoolWorker-2] reply:
[2023-07-20 17:03:06,317: WARNING/ForkPoolWorker-2] b'421 4.7.0 Try again later, closing connection. (EHLO) v5-20020a056e0202c500b003465bd4755csm87707ilr.54 - gsmtp\r\n'
[2023-07-20 17:03:06,317: WARNING/ForkPoolWorker-2] reply: retcode (421); Msg: b'4.7.0 Try again later, closing connection. (EHLO) v5-20020a056e0202c500b003465bd4755csm87707ilr.54 - gsmtp'
[2023-07-20 17:03:06,317: WARNING/ForkPoolWorker-2] send:
[2023-07-20 17:03:06,317: WARNING/ForkPoolWorker-2] 'helo [127.0.0.1]\r\n'
[2023-07-20 17:03:06,318: ERROR/ForkPoolWorker-2] Connection unexpectedly closed
Traceback (most recent call last):
  ...
  ...
  File ".../helpers/mailer.py", line 38, in send
    code, response = connection.helo()
  File "/usr/lib/python3.6/smtplib.py", line 430, in helo
    (code, msg) = self.getreply()
  File "/usr/lib/python3.6/smtplib.py", line 394, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
[2023-07-20 17:03:06,322: WARNING/ForkPoolWorker-2] Email sending was failed, retryin

When I manually supply the local_hostname to the base Python SMTP service init method, it works as expected

connection = self.smtp(hostname, port, timeout=timeout, local_hostname=<actual server name>)

May I request that you accept local_hostname in the SMTPMailer class and forward this parameter to the base Python SMTP class please?

class SMTPMailer(object):

    smtp = SMTP  # allow replacement for testing.
    smtp_ssl = SMTP_SSL # allow replacement for testing.

    def __init__(self, hostname='localhost', port=25,
                 username=None, password=None,
                 no_tls=False, force_tls=False, ssl=False, debug_smtp=False):    <---- Accept local_hostname here
        self.hostname = hostname
        self.port = port
        self.username = username
        self.password = password
        self.force_tls = force_tls
        self.no_tls = no_tls
        self.ssl = ssl
        self.debug_smtp = debug_smtp

    def smtp_factory(self):
        hostname = self.hostname
        port = str(self.port)
        timeout = 10
        if self.ssl:
            if self.smtp_ssl is None:
                raise RuntimeError('No SSL available, cannot send via SSL')
            connection = self.smtp_ssl(hostname, port, timeout=timeout)  <------- Pass local_hostname here
        else:
            connection = self.smtp(hostname, port, timeout=timeout) <------- Pass local_hostname here
        connection.set_debuglevel(self.debug_smtp)
        return connection

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions