idna-encode emails on sent
This commit is contained in:
parent
c6beedbc5d
commit
b4e725bbdc
|
@ -1,11 +1,10 @@
|
|||
# encoding: utf-8
|
||||
|
||||
__all__ = ['SMTPClientWithResponse', 'SMTPClientWithResponse_SSL']
|
||||
|
||||
import smtplib
|
||||
from smtplib import _have_ssl, SMTP
|
||||
import logging
|
||||
|
||||
from ... utils import sanitize_email
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
@ -67,6 +66,8 @@ class SMTPClientWithResponse(SMTP):
|
|||
|
||||
response = self.make_response()
|
||||
|
||||
from_addr = sanitize_email(from_addr)
|
||||
|
||||
response.from_addr = from_addr
|
||||
response.esmtp_opts = esmtp_opts[:]
|
||||
|
||||
|
@ -82,6 +83,8 @@ class SMTPClientWithResponse(SMTP):
|
|||
if not isinstance(to_addrs, (list, tuple)):
|
||||
to_addrs = [to_addrs]
|
||||
|
||||
to_addrs = [sanitize_email(e) for e in to_addrs]
|
||||
|
||||
response.to_addrs = to_addrs
|
||||
response.rcpt_options = rcpt_options[:]
|
||||
response.refused_recipients = {}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
from __future__ import absolute_import
|
||||
from django.core.mail import get_connection
|
||||
from .. message import MessageTransformerMixin, MessageSignMixin, MessageBuildMixin, BaseMessage
|
||||
from .. utils import sanitize_email
|
||||
|
||||
__all__ = ['DjangoMessageMixin', 'DjangoMessage']
|
||||
|
||||
|
@ -15,13 +16,12 @@ class DjangoMessageMixin(object):
|
|||
return self.charset or 'utf-8'
|
||||
|
||||
def recipients(self):
|
||||
if self._recipients is not None:
|
||||
return self._recipients
|
||||
return [r[1] for r in self.mail_to]
|
||||
r = self._recipients if self._recipients is not None else [r[1] for r in self.mail_to]
|
||||
return [sanitize_email(e) for e in r]
|
||||
|
||||
@property
|
||||
def from_email(self):
|
||||
return self._from_email or self.mail_from[1]
|
||||
return sanitize_email(self._from_email or self.mail_from[1])
|
||||
|
||||
def _set_emails(self, mail_to=None, set_mail_to=True, mail_from=None,
|
||||
set_mail_from=False, to=None):
|
||||
|
|
|
@ -136,7 +136,9 @@ def parse_name_and_email(obj, encoding='utf-8'):
|
|||
return to_unicode(name, encoding) or None, to_unicode(email, encoding) or None
|
||||
|
||||
|
||||
def sanitize_email(addr, encoding='ascii'):
|
||||
def sanitize_email(addr, encoding='ascii', parse=False):
|
||||
if parse:
|
||||
_, addr = parseaddr(to_unicode(addr))
|
||||
try:
|
||||
addr.encode('ascii')
|
||||
except UnicodeEncodeError: # IDN
|
||||
|
@ -160,7 +162,7 @@ def sanitize_address(addr, encoding='ascii'):
|
|||
nm = Header(nm, encoding).encode()
|
||||
except UnicodeEncodeError:
|
||||
nm = Header(nm, 'utf-8').encode()
|
||||
return formataddr((nm, sanitize_email(addr)))
|
||||
return formataddr((nm, sanitize_email(addr, encoding=encoding, parse=False)))
|
||||
|
||||
|
||||
class MIMEMixin():
|
||||
|
|
Reference in New Issue