I am using django 3.0.8 and in my settings.py, I've specified the password for my e-mail account using EMAIL_PASSWORD = '...'. My password contains umlauts and upon manually sending a mail from the shell I get this error:
>>> from django.core.mail import send_mail
>>> send_mail('Django mail', 'This e-mail was sent with django', ..., fail_silently=False)
Traceback (most recent call last):
File "/usr/lib/python3.6/code.py", line 91, in runcode
exec(code, self.locals)
File "<console>", line 1, in <module>
File "/home/admin/.local/lib/python3.6/site-packages/django/core/mail/__init__.py", line 60, in send_mail
return mail.send()
File "/home/admin/.local/lib/python3.6/site-packages/django/core/mail/message.py", line 276, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/admin/.local/lib/python3.6/site-packages/django/core/mail/backends/smtp.py", line 102, in send_messages
new_conn_created = self.open()
File "/home/admin/.local/lib/python3.6/site-packages/django/core/mail/backends/smtp.py", line 69, in open
self.connection.login(self.username, self.password)
File "/usr/lib/python3.6/smtplib.py", line 721, in login
initial_response_ok=initial_response_ok)
File "/usr/lib/python3.6/smtplib.py", line 630, in auth
response = encode_base64(initial_response.encode('ascii'), eol='')
UnicodeEncodeError: 'ascii' codec can't encode character '\xe4' in position 19: ordinal not in range(128)
If I remove the umlaut everything works as it should. Apparently smtplib manually encodes with ascii and I don't know how to tell it not to. Any ideas?
This is a known issue in smtplib.
As for now, July 2020 its is still open, with the fix awaiting review.
Bottom line - there is a problem, it is known, there is no official solution.
And yet, what you can do for now:
Manually patch the file /usr/lib/python3.6/smtplib.py
the way it is done here - it is very simple, just replace ascii
with utf-8
in 3 places. I didn't try it but it works according to other users. Just backup the file just in case. Or use a virtual env with a patched smtplib.py. To be honest I personally didn't test it but people report it working.
Use some alternative to smtplib. I actually don't know anything like this in native Python, however you can always use command line utilities like mail
via python subprocess
module, or some web services that provide mailing service via REST API (there are plenty, but this will not work if you are using mail on local company network or something).
Change your password and forget about the problem.