Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
roryodonnell opened this issue Jul 25, 2023 · 0 comments
Open

Comments

@roryodonnell
Copy link

roryodonnell commented Jul 25, 2023

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
@roryodonnell roryodonnell changed the title SMTPMailer not passing 'local_hostname' to base bass SMTP class SMTPMailer not passing 'local_hostname' to base Python SMTP class Jul 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant