简体   繁体   中英

Django 1.11 - How to send mails using a mail server which supports NTLM authentication only

I'm using Django 1.11 and want to send mails by using 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

These settings are working fine using a SMTP testing tool called Swaks. Does anybody have experience with using Exchange and NTLM authentication for sending emails with Django 1.11? 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.

Debugging of the smtplib revealed the actual error response by Exchange:

resp: 5.7.1 This message has been blocked because the HELO/EHLO domain is invalid.

It turned out that Exchange checks the name and/or domain of the sending host. My host name was the random container ID assigned by Docker. I had to change the FQDN to something real (like mail.google.com ) in the Docker Compose file and then it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM