Python 2.7 on our servers recently received an update to fix a problem with email.parseaddr()
:
[root@opal4 ~]# rpm -q --changelog python | head
* Mon Mar 04 2024 Dmitriy Popov <dpopov@cloudlinux.com> - 2.7.5-94.tuxcare.els2
- CVE-2023-27043: reject malformed addresses in email.parseaddr()
...
Unfortunately this breaks old Django apps running on Python 2.7 because django.core.mail.message.sanitize_address
converts addresses to Unicode and it seems the updated parseaddr
function doesn't recognize Unicode email addresses.
If you're running an old Django app on Python 2.7 and suddenly receiving errors like "Bad recipient address syntax" when sending mail from Django then this is the likely cause. I really recommend you upgrade your app to the latest versions of Django and Python but if you're not able to upgrade then you can fix the problem manually.
To fix the problem you must edit the file ~/apps/YOUR_APP/env/lib/python2.7/site-packages/django/core/mail/message.py
to change the following line in the sanitize_address
function from this:
addr = parseaddr(force_text(addr))
... to this:
addr = parseaddr(str(addr))
Note that the code in very old Django apps may be different so you'll need to adapt the above to whatever you have. For example in Django 1.0a, the file is ~/apps/YOUR_APP/env/lib/python2.7/site-packages/django/core/mail.py
and the fix is to change line 183 from this...
email_message.recipients(),
... to this:
[str(e) for e in email_message.recipients()],
In either case, restart your app after making the changes and it should then be able to send mail again.