Django邮件SMTPlib身份验证方法问题

发布于 2024-11-08 01:03:16 字数 426 浏览 0 评论 0原文

我正在尝试使用 Django 和我客户的 smtp 服务器发送电子邮件。 该服务器仅支持 AUTH LOGIN 命令。

默认情况下(据我使用wireshark所见)django发送此位:

250-AUTH=LOGIN CRAM-MD5 PLAIN

作为电子邮件的一部分。

我已将其跟踪到 /usr/lib/python/smtplib.py:569 ,它在其中选择身份验证方法(有一个 AUTH_LOGIN 选项,这就是我想要的)。

据我所知,我无法使用 settings.py 中的变量设置身份验证方法(http://docs.djangoproject.com/en/dev/topics/email/)

有谁知道我如何告诉 Django 使用auth_login 而不是 auth_cram_md5?

I am trying to send an email using Django and my client's smtp server.
That server supports AUTH LOGIN commands only.

By default (as far as i can see using wireshark) django sends this bit:

250-AUTH=LOGIN CRAM-MD5 PLAIN

as part of the email message.

I have tracked that to /usr/lib/python/smtplib.py:569 where it chooses the authentication method (there's an option for AUTH_LOGIN which is what i want).

As far as i can see i can't set the auth method using vars in settings.py (http://docs.djangoproject.com/en/dev/topics/email/)

Does anyone know how i can tell Django to use auth_login instead of auth_cram_md5?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单挑你×的.吻 2024-11-15 01:03:16

我找到了一种方法来告诉 smtplib 使用一种特定的身份验证机制(归功于 Python 的 SMTP AUTH 扩展问题)。下面的代码行允许您向 smtplib 建议身份验证机制,忽略服务器提供的其他机制(例如 CRAM-MD5 DIGEST-MD5):

server.esmtp_features['auth'] = 'LOGIN PLAIN'

在许多教程中发现的另一个常见错误是,当使用 .starttls()< /code>,需要发送 server.ehlo() 两次,一次在 .starttls() 之前,一次在 .starttls() 之后。

这是对我有用的代码:

import smtplib

user = '<username>'
pwd = '<password>'

FROM = '[email protected]'
TO = ['[email protected]']
SUBJECT = 'Testing sending using gmail'
TEXT = 'Testing sending mail using gmail servers'

# Prepare actual message
message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

try:
    server = smtplib.SMTP("<SMTP-Server-URI>", 25)
    server.set_debuglevel(1) # optional, good for debugging
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.esmtp_features['auth'] = 'LOGIN PLAIN'
    server.login(user, pwd)
    server.sendmail(FROM, TO, message)
    server.close()
    print 'successfully sent the mail'
except:
    print 'failed to send mail'

此代码仅使用 Python 2.7.9 进行了测试(而不是专门使用 Django)。希望它能帮助一些人。

I found a way to tell smtplib to use one specific authentication mechanism (credit goes to SMTP AUTH extension trouble with Python). The following line of code allows you to suggest authentication mechanisms to smtplib, ignoring other mechanisms offered by the server (e.g. CRAM-MD5 DIGEST-MD5):

server.esmtp_features['auth'] = 'LOGIN PLAIN'

Another common mistake found on many tutorials is that when using .starttls(), one needs to send the server.ehlo() twice, once before and once after the .starttls().

Here is the code which worked for me:

import smtplib

user = '<username>'
pwd = '<password>'

FROM = '[email protected]'
TO = ['[email protected]']
SUBJECT = 'Testing sending using gmail'
TEXT = 'Testing sending mail using gmail servers'

# Prepare actual message
message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

try:
    server = smtplib.SMTP("<SMTP-Server-URI>", 25)
    server.set_debuglevel(1) # optional, good for debugging
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.esmtp_features['auth'] = 'LOGIN PLAIN'
    server.login(user, pwd)
    server.sendmail(FROM, TO, message)
    server.close()
    print 'successfully sent the mail'
except:
    print 'failed to send mail'

This code was only tested with Python 2.7.9 (and not specifically with Django). Hope it helps some people.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文