Fixed #7722 - added support for CC in EmailMessage.
Thanks to roberto.digirolamo for the report and initial patch, and dougvanhorn and SmileyChris for further work on the patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@14000 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
2faa490aeb
commit
0aa438a3df
|
@ -105,7 +105,7 @@ class EmailMessage(object):
|
||||||
encoding = None # None => use settings default
|
encoding = None # None => use settings default
|
||||||
|
|
||||||
def __init__(self, subject='', body='', from_email=None, to=None, bcc=None,
|
def __init__(self, subject='', body='', from_email=None, to=None, bcc=None,
|
||||||
connection=None, attachments=None, headers=None):
|
connection=None, attachments=None, headers=None, cc=None):
|
||||||
"""
|
"""
|
||||||
Initialize a single email message (which can be sent to multiple
|
Initialize a single email message (which can be sent to multiple
|
||||||
recipients).
|
recipients).
|
||||||
|
@ -119,6 +119,11 @@ class EmailMessage(object):
|
||||||
self.to = list(to)
|
self.to = list(to)
|
||||||
else:
|
else:
|
||||||
self.to = []
|
self.to = []
|
||||||
|
if cc:
|
||||||
|
assert not isinstance(cc, basestring), '"cc" argument must be a list or tuple'
|
||||||
|
self.cc = list(cc)
|
||||||
|
else:
|
||||||
|
self.cc = []
|
||||||
if bcc:
|
if bcc:
|
||||||
assert not isinstance(bcc, basestring), '"bcc" argument must be a list or tuple'
|
assert not isinstance(bcc, basestring), '"bcc" argument must be a list or tuple'
|
||||||
self.bcc = list(bcc)
|
self.bcc = list(bcc)
|
||||||
|
@ -145,6 +150,8 @@ class EmailMessage(object):
|
||||||
msg['Subject'] = self.subject
|
msg['Subject'] = self.subject
|
||||||
msg['From'] = self.extra_headers.get('From', self.from_email)
|
msg['From'] = self.extra_headers.get('From', self.from_email)
|
||||||
msg['To'] = ', '.join(self.to)
|
msg['To'] = ', '.join(self.to)
|
||||||
|
if self.cc:
|
||||||
|
msg['Cc'] = ', '.join(self.cc)
|
||||||
|
|
||||||
# Email header names are case-insensitive (RFC 2045), so we have to
|
# Email header names are case-insensitive (RFC 2045), so we have to
|
||||||
# accommodate that when doing comparisons.
|
# accommodate that when doing comparisons.
|
||||||
|
@ -162,9 +169,9 @@ class EmailMessage(object):
|
||||||
def recipients(self):
|
def recipients(self):
|
||||||
"""
|
"""
|
||||||
Returns a list of all recipients of the email (includes direct
|
Returns a list of all recipients of the email (includes direct
|
||||||
addressees as well as Bcc entries).
|
addressees as well as Cc and Bcc entries).
|
||||||
"""
|
"""
|
||||||
return self.to + self.bcc
|
return self.to + self.cc + self.bcc
|
||||||
|
|
||||||
def send(self, fail_silently=False):
|
def send(self, fail_silently=False):
|
||||||
"""Sends the email message."""
|
"""Sends the email message."""
|
||||||
|
@ -252,7 +259,8 @@ class EmailMultiAlternatives(EmailMessage):
|
||||||
alternative_subtype = 'alternative'
|
alternative_subtype = 'alternative'
|
||||||
|
|
||||||
def __init__(self, subject='', body='', from_email=None, to=None, bcc=None,
|
def __init__(self, subject='', body='', from_email=None, to=None, bcc=None,
|
||||||
connection=None, attachments=None, headers=None, alternatives=None):
|
connection=None, attachments=None, headers=None, alternatives=None,
|
||||||
|
cc=None):
|
||||||
"""
|
"""
|
||||||
Initialize a single email message (which can be sent to multiple
|
Initialize a single email message (which can be sent to multiple
|
||||||
recipients).
|
recipients).
|
||||||
|
@ -261,7 +269,7 @@ class EmailMultiAlternatives(EmailMessage):
|
||||||
bytestrings). The SafeMIMEText class will handle any necessary encoding
|
bytestrings). The SafeMIMEText class will handle any necessary encoding
|
||||||
conversions.
|
conversions.
|
||||||
"""
|
"""
|
||||||
super(EmailMultiAlternatives, self).__init__(subject, body, from_email, to, bcc, connection, attachments, headers)
|
super(EmailMultiAlternatives, self).__init__(subject, body, from_email, to, bcc, connection, attachments, headers, cc)
|
||||||
self.alternatives=alternatives or []
|
self.alternatives=alternatives or []
|
||||||
|
|
||||||
def attach_alternative(self, content, mimetype):
|
def attach_alternative(self, content, mimetype):
|
||||||
|
|
|
@ -235,6 +235,9 @@ following parameters (in the given order, if positional arguments are used).
|
||||||
All parameters are optional and can be set at any time prior to calling the
|
All parameters are optional and can be set at any time prior to calling the
|
||||||
``send()`` method.
|
``send()`` method.
|
||||||
|
|
||||||
|
.. versionchanged:: 1.3
|
||||||
|
The ``cc`` argument was added.
|
||||||
|
|
||||||
* ``subject``: The subject line of the e-mail.
|
* ``subject``: The subject line of the e-mail.
|
||||||
|
|
||||||
* ``body``: The body text. This should be a plain text message.
|
* ``body``: The body text. This should be a plain text message.
|
||||||
|
@ -261,6 +264,9 @@ All parameters are optional and can be set at any time prior to calling the
|
||||||
caller to ensure header names and values are in the correct format for
|
caller to ensure header names and values are in the correct format for
|
||||||
an e-mail message.
|
an e-mail message.
|
||||||
|
|
||||||
|
* ``cc``: A list or tuple of recipient addresses used in the "Cc" header
|
||||||
|
when sending the e-mail.
|
||||||
|
|
||||||
For example::
|
For example::
|
||||||
|
|
||||||
email = EmailMessage('Hello', 'Body goes here', 'from@example.com',
|
email = EmailMessage('Hello', 'Body goes here', 'from@example.com',
|
||||||
|
|
|
@ -417,4 +417,30 @@ Content
|
||||||
>>> settings.ADMINS = old_admins
|
>>> settings.ADMINS = old_admins
|
||||||
>>> settings.MANAGERS = old_managers
|
>>> settings.MANAGERS = old_managers
|
||||||
|
|
||||||
|
# Add Cc to the email argument list (#7722)
|
||||||
|
|
||||||
|
>>> email = EmailMessage('Subject', 'Content', 'from@example.com', ['to@example.com'], cc=['cc@example.com'])
|
||||||
|
>>> message = email.message()
|
||||||
|
>>> message['Cc']
|
||||||
|
'cc@example.com'
|
||||||
|
>>> email.recipients()
|
||||||
|
['to@example.com', 'cc@example.com']
|
||||||
|
|
||||||
|
>>> email = EmailMessage('Subject', 'Content', 'from@example.com', ['to@example.com','other@example.com'], cc=['cc@example.com', 'cc.other@example.com'])
|
||||||
|
>>> message = email.message()
|
||||||
|
>>> message['Cc']
|
||||||
|
'cc@example.com, cc.other@example.com'
|
||||||
|
>>> email.recipients()
|
||||||
|
['to@example.com', 'other@example.com', 'cc@example.com', 'cc.other@example.com']
|
||||||
|
|
||||||
|
>>> email = EmailMessage('Subject', 'Content', 'from@example.com', ['to@example.com','other@example.com'], cc=['cc@example.com', 'cc.other@example.com'], bcc=['bcc@example.com'])
|
||||||
|
>>> message = email.message()
|
||||||
|
>>> email.recipients()
|
||||||
|
['to@example.com', 'other@example.com', 'cc@example.com', 'cc.other@example.com', 'bcc@example.com']
|
||||||
|
|
||||||
|
>>> email = EmailMessage('Subject', 'Content', 'from@example.com', ['to@example.com'], cc=['cc@example.com'])
|
||||||
|
>>> message = email.message()
|
||||||
|
>>> message.as_string()
|
||||||
|
'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Subject\nFrom: from@example.com\nTo: to@example.com\nCc: cc@example.com\nDate: ...\nMessage-ID: <...>\n\nContent'
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue