0

I'm trying to send an e-mail to the user on registration using taigaio, which is based on Django.

Here are my current SMTP settings (I didn't yet switched to SSL but I'm planning to):

DEFAULT_FROM_EMAIL=noreply@server.org
EMAIL_HOST_USER=noreply
EMAIL_HOST_PASSWORD="thesupersecretpassword"
EMAIL_HOST=smtp.server.org
EMAIL_PORT=25
EMAIL_USE_TLS=False
EMAIL_USE_SSL=False

But the browser raises a 500 Internal Server Error and I got on the server side:

(...)
File "/taiga-back/taiga/auth/services.py", line 62, in send_register_email
  return bool(email.send())
File "/opt/venv/lib/python3.7/site-packages/django/core/mail/message.py", line 306, in send
  return self.get_connection(fail_silently).send_messages([self])
File "/opt/venv/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 103, in send_messages
  new_conn_created = self.open()
File "/opt/venv/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 70, in open
  self.connection.login(self.username, self.password)
File "/usr/local/lib/python3.7/smtplib.py", line 710, in login
  raise SMTPException("No suitable authentication method found.")
smtplib.SMTPException: No suitable authentication method found.

I also tried to simply send a mail using smtplib as follow (inspired from https://mkyong.com/python/how-do-send-email-in-python-via-smtplib/) to see if I can figure out what's wrong:

import smtplib

to = 'me@gmail.com'
email_user = 'noreply'
smtpserver = smtplib.SMTP("smtp.server.org", 25)

header = 'To:' + to + '\n' + 'From: ' + email_user + '\n' + 'Subject:testing \n'
msg = header + '\n Sending test message. from noreply@server.org \n\n'
smtpserver.sendmail(email_user, to, msg)
smtpserver.close()

And it works fine, I do receive that e-mail.

Did I miss something in the Django config for sending e-mails?

Edit:

If I set EMAIL_USE_TLS=True as suggested on the comment, and use EMAIL_PORT=587 I face this error then:

smtplib.SMTPException: No suitable authentication method found.

But in that case, even the simple call to smtplib doesn't work:

smtpserver = smtplib.SMTP("smtp.server.org", 587)

This leads to:

>>> smtpserver.sendmail(email_user, to, msg)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/smtplib.py", line 867, in sendmail
    raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (530, b'Must issue a STARTTLS command first', 'noreply@server.org')

Therefore, I also tried using smtplib.SMTP_SSL() as follow:

import smtplib

to = 'me@gmail.com'
email_user = 'noreply'
email_pwd = "thesupersecretpassword"
smtpserver = smtplib.SMTP_SSL("smtp.server.org", 587) # <-- it fails here!

header = 'To:' + to + '\n' + 'From: ' + email_user + '\n' + 'Subject:testing \n'
msg = header + '\n Sending test message. from noreply@server.org \n\n'
smtpserver.sendmail(email_user, to, msg)
smtpserver.close()

which fails on:

>>> smtpserver = smtplib.SMTP_SSL("smtp.server.org", 587)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/smtplib.py", line 1031, in __init__
    source_address)
  File "/usr/local/lib/python3.7/smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/local/lib/python3.7/smtplib.py", line 336, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/local/lib/python3.7/smtplib.py", line 1039, in _get_socket
    server_hostname=self._host)
  File "/usr/local/lib/python3.7/ssl.py", line 423, in wrap_socket
    session=session
  File "/usr/local/lib/python3.7/ssl.py", line 870, in _create
    self.do_handshake()
  File "/usr/local/lib/python3.7/ssl.py", line 1139, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1091)

So, I went here: How to fix ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)? and tried the given accepted answer, but without success:

>>> server.ehlo()
(250, b'smtp.server.org Hello [10.192.xxx.yyy]\nSIZE 37748736\nPIPELINING\nDSN\nENHANCEDSTATUSCODES\nSTARTTLS\nAUTH GSSAPI NTLM\n8BITMIME\nBINARYMIME\nCHUNKING')
>>> server.starttls(context=context)
(220, b'Ready to start TLS')
>>> server.ehlo()
(250, b'smtp.server.org Hello [10.192.xxx.yyy]\nSIZE 37748736\nPIPELINING\nDSN\nENHANCEDSTATUSCODES\nSTARTTLS\nAUTH GSSAPI NTLM\n8BITMIME\nBINARYMIME\nCHUNKING')
>>> server.login(sender_email, password)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/smtplib.py", line 710, in login
    raise SMTPException("No suitable authentication method found.")
smtplib.SMTPException: No suitable authentication method found.

This exact same error smtplib.SMTPException: No suitable authentication method found. also occurs when using the Django e-mail backend using the same parameters, as stated at the beginning of this edit section.

Precision: I'm not using gmail for sending the e-mail. It's an internal institutional server.

swiss_knight
  • 5,787
  • 8
  • 50
  • 92

0 Answers0