Monday, April 20, 2020

Sending emails using Exchange 2013 server and NTLM authentication

Dear all,

I'm using Django 1.11 and want to send mails by utilizing an Exchange 2013 server which only supports NTLM for SMTP authentification. I realized that the default email backend `django.core.mail.backends.smtp.EmailBackend` only supports LOGIN, PLAIN or CRAM-MD5 for authentication. Luckily, I found another promising backend (https://github.com/shadiakiki1986/django-smtp-ntlm-backend) for SMTP with NTLM authentication. Installation of the backend was successful but it does not work. 

The following happens at the Python console:

>>> from django.core.mail import send_mail
>>> send_mail('test subject', 'message', 'email@address.com', ['recipient@gmail.com'], fail_silently=False,)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/django/core/mail/__init__.py", line 62, in send_mail
    return mail.send()
  File "/usr/local/lib/python2.7/site-packages/django/core/mail/message.py", line 348, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/usr/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 111, in send_messages
    sent = self._send(message)
  File "/usr/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 127, in _send
    self.connection.sendmail(from_email, recipients, message.as_bytes(linesep='\r\n'))
  File "smtplib.py", line 736, in sendmail
    self.rset()
  File "smtplib.py", line 470, in rset
    return self.docmd("rset")
  File "smtplib.py", line 395, in docmd
    return self.getreply()
  File "smtplib.py", line 369, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed


Relevant settings in settings.py:

EMAIL_ENABLE=True
EMAIL_BACKEND=django_smtp_ntlm_backendNTLMEmail 
EMAIL_HOST=<mailserver>
EMAIL_PORT=25
EMAIL_HOST_USER=<domain>\<user>
EMAIL_HOST_PASSWORD=<password>
DEFAULT_FROM_EMAIL=<email>
EMAIL_USE_SSL=False                                                                                                                                                  
EMAIL_USE_TLS=False


The settings for the mail server are correct and the mail server is working fine. I am able to send mails with a SMTP testing tool called Swaks with the same settings using the same mail server. Does anybody has experience with using Exchange and NTLM authentication for sending emails with Django 1.11? Maybe there is another email backend I can use? I can imagine that things might be easier with a newer Django version and Python 3 because of changes in the underlying smtplib, but I'm stuck to Django 1.11. I'm grateful for any hints.

Best regards
Fabian

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/27506d71-b2d1-43d5-8d2f-209da429e160%40googlegroups.com.

No comments:

Post a Comment