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:
Luke Plant 2010-10-07 23:36:02 +00:00
parent 2faa490aeb
commit 0aa438a3df
3 changed files with 45 additions and 5 deletions

View File

@ -105,7 +105,7 @@ class EmailMessage(object):
encoding = None # None => use settings default
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
recipients).
@ -119,6 +119,11 @@ class EmailMessage(object):
self.to = list(to)
else:
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:
assert not isinstance(bcc, basestring), '"bcc" argument must be a list or tuple'
self.bcc = list(bcc)
@ -145,6 +150,8 @@ class EmailMessage(object):
msg['Subject'] = self.subject
msg['From'] = self.extra_headers.get('From', self.from_email)
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
# accommodate that when doing comparisons.
@ -162,9 +169,9 @@ class EmailMessage(object):
def recipients(self):
"""
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):
"""Sends the email message."""
@ -252,7 +259,8 @@ class EmailMultiAlternatives(EmailMessage):
alternative_subtype = 'alternative'
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
recipients).
@ -261,7 +269,7 @@ class EmailMultiAlternatives(EmailMessage):
bytestrings). The SafeMIMEText class will handle any necessary encoding
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 []
def attach_alternative(self, content, mimetype):

View File

@ -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
``send()`` method.
.. versionchanged:: 1.3
The ``cc`` argument was added.
* ``subject``: The subject line of the e-mail.
* ``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
an e-mail message.
* ``cc``: A list or tuple of recipient addresses used in the "Cc" header
when sending the e-mail.
For example::
email = EmailMessage('Hello', 'Body goes here', 'from@example.com',

View File

@ -417,4 +417,30 @@ Content
>>> settings.ADMINS = old_admins
>>> 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'
"""