#27131 closed Bug (fixed)
send_mail() error on Python 2 if smtp server uses CRAM-MD5 auth method
| Reported by: | Slava | Owned by: | nobody |
|---|---|---|---|
| Component: | Core (Mail) | Version: | dev |
| Severity: | Normal | Keywords: | send_mail |
| Cc: | slavugan@… | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
send_mail('title', 'message', from_email='test@mail.com', recipient_list=['test2@mail.com'])
Traceback (most recent call last):
File "<input>", line 1, in <module>
send_mail('hello slafffko', 'message is here', from_email='test@artel7.com',
recipient_list=['slavugan@gmail.com'])
File "/home/slav/venv/luxjango/local/lib/python2.7/site-packages/django/core/m
ail/__init__.py", line 62, in send_mail
return mail.send()
File "/home/slav/venv/luxjango/local/lib/python2.7/site-packages/django/core/m
ail/message.py", line 303, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/slav/venv/luxjango/local/lib/python2.7/site-packages/django/core/m
ail/backends/smtp.py", line 100, in send_messages
new_conn_created = self.open()
File "/home/slav/venv/luxjango/local/lib/python2.7/site-packages/django/core/m
ail/backends/smtp.py", line 67, in open
self.connection.login(self.username, self.password)
File "/usr/lib/python2.7/smtplib.py", line 607, in login
(code, resp) = self.docmd(encode_cram_md5(resp, user, password))
File "/usr/lib/python2.7/smtplib.py", line 571, in encode_cram_md5
response = user + " " + hmac.HMAC(password, challenge).hexdigest()
File "/usr/lib/python2.7/hmac.py", line 75, in __init__
self.outer.update(key.translate(trans_5C))
TypeError: character mapping must return integer, None or unicode
email backend should pass passwrord as string, not as unicode to smtplib to avoid this error
Change History (11)
comment:1 by , 9 years ago
comment:4 by , 9 years ago
I have checked for python3 everything is ok, because of with python3 Django passes password as string to smtplib, so this error is relevant only for python2.
For fix in django/core/mail/backends/smtp.py in EmailBackend.__init__ we should add something like this:
if self.password.__class__.__name__ == 'unicode':
try:
self.password = str(self.password)
except UnicodeEncodeError:
pass
comment:5 by , 9 years ago
| Has patch: | set |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
| Type: | Uncategorized → Bug |
| Version: | 1.8 → master |
comment:6 by , 9 years ago
| Summary: | send_mail error if smtp server uses CRAM-MD5 auth method → send_mail() error on Python 2 if smtp server uses CRAM-MD5 auth method |
|---|---|
| Triage Stage: | Accepted → Ready for checkin |
comment:8 by , 9 years ago
As suggested by Tim on the pull request, a workaround on older Django could be to define email username and pasword as bytestrings.
Note:
See TracTickets
for help on using tickets.
I have no way to reproduce this -- can you submit a patch with a test?