Fixed #30608 -- Fixed non-unicode EmailMessage crash when domain name for localhost is non-ASCII.

Assisted by felixxm.
This commit is contained in:
Chason Chaffin 2019-07-02 21:15:32 +09:00 committed by Mariusz Felisiak
parent f226bdbf4e
commit 55b68de643
3 changed files with 13 additions and 2 deletions

View File

@ -166,6 +166,7 @@ answer newbie questions, and generally made Django that much better:
ChaosKCW ChaosKCW
Charlie Leifer <coleifer@gmail.com> Charlie Leifer <coleifer@gmail.com>
charly.wilhelm@gmail.com charly.wilhelm@gmail.com
Chason Chaffin <chason@gmail.com>
Cheng Zhang Cheng Zhang
Chris Adams Chris Adams
Chris Beaven <smileychris@gmail.com> Chris Beaven <smileychris@gmail.com>

View File

@ -4,6 +4,8 @@ Email message and email sending related helper functions.
import socket import socket
from django.utils.encoding import punycode
# Cache the hostname, but do it lazily: socket.getfqdn() can take a couple of # Cache the hostname, but do it lazily: socket.getfqdn() can take a couple of
# seconds, which slows down the restart of the server. # seconds, which slows down the restart of the server.
@ -13,7 +15,7 @@ class CachedDnsName:
def get_fqdn(self): def get_fqdn(self):
if not hasattr(self, '_fqdn'): if not hasattr(self, '_fqdn'):
self._fqdn = socket.getfqdn() self._fqdn = punycode(socket.getfqdn())
return self._fqdn return self._fqdn

View File

@ -14,10 +14,11 @@ from email.utils import parseaddr
from io import StringIO from io import StringIO
from smtplib import SMTP, SMTPAuthenticationError, SMTPException from smtplib import SMTP, SMTPAuthenticationError, SMTPException
from ssl import SSLError from ssl import SSLError
from unittest import mock
from django.core import mail from django.core import mail
from django.core.mail import ( from django.core.mail import (
EmailMessage, EmailMultiAlternatives, mail_admins, mail_managers, DNS_NAME, EmailMessage, EmailMultiAlternatives, mail_admins, mail_managers,
send_mail, send_mass_mail, send_mail, send_mass_mail,
) )
from django.core.mail.backends import console, dummy, filebased, locmem, smtp from django.core.mail.backends import console, dummy, filebased, locmem, smtp
@ -365,6 +366,13 @@ class MailTests(HeadersCheckMixin, SimpleTestCase):
self.assertEqual(msg.body, '') self.assertEqual(msg.body, '')
self.assertEqual(msg.message().get_payload(), '') self.assertEqual(msg.message().get_payload(), '')
@mock.patch('socket.getfqdn', return_value='漢字')
def test_non_ascii_dns_non_unicode_email(self, mocked_getfqdn):
delattr(DNS_NAME, '_fqdn')
email = EmailMessage('subject', 'content', 'from@example.com', ['to@example.com'])
email.encoding = 'iso-8859-1'
self.assertIn('@xn--p8s937b>', email.message()['Message-ID'])
def test_encoding(self): def test_encoding(self):
""" """
Regression for #12791 - Encode body correctly with other encodings Regression for #12791 - Encode body correctly with other encodings