Skip to content

Commit

Permalink
Fixed #3307 -- Added BCC support to the EmailMessage class. En-passan…
Browse files Browse the repository at this point in the history
…t, fixed a

number of RST formatting errors in the email docs.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@5146 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
malcolmt committed May 3, 2007
1 parent bc6535b commit 5ff1d96
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 31 deletions.
20 changes: 14 additions & 6 deletions django/core/mail.py
Expand Up @@ -142,7 +142,8 @@ def _send(self, email_message):
return False return False
try: try:
self.connection.sendmail(email_message.from_email, self.connection.sendmail(email_message.from_email,
email_message.to, email_message.message().as_string()) email_message.recipients(),
email_message.message().as_string())
except: except:
if not self.fail_silently: if not self.fail_silently:
raise raise
Expand All @@ -153,12 +154,10 @@ class EmailMessage(object):
""" """
A container for email information. A container for email information.
""" """
def __init__(self, subject='', body='', from_email=None, to=None, connection=None): def __init__(self, subject='', body='', from_email=None, to=None, bcc=None, connection=None):
self.to = to or [] self.to = to or []
if from_email is None: self.bcc = bcc or []
self.from_email = settings.DEFAULT_FROM_EMAIL self.from_email = from_email or settings.DEFAULT_FROM_EMAIL
else:
self.from_email = from_email
self.subject = subject self.subject = subject
self.body = body self.body = body
self.connection = connection self.connection = connection
Expand All @@ -175,8 +174,17 @@ def message(self):
msg['To'] = ', '.join(self.to) msg['To'] = ', '.join(self.to)
msg['Date'] = formatdate() msg['Date'] = formatdate()
msg['Message-ID'] = make_msgid() msg['Message-ID'] = make_msgid()
if self.bcc:
msg['Bcc'] = ', '.join(self.bcc)
return msg return msg


def recipients(self):
"""
Returns a list of all recipients of the email (includes direct
addressees as well as Bcc entries).
"""
return self.to + self.bcc

def send(self, fail_silently=False): def send(self, fail_silently=False):
"""Send the email message.""" """Send the email message."""
return self.get_connection(fail_silently).send_messages([self]) return self.get_connection(fail_silently).send_messages([self])
Expand Down
64 changes: 39 additions & 25 deletions docs/email.txt
Expand Up @@ -189,42 +189,56 @@ from the request's POST data, sends that to admin@example.com and redirects to
The EmailMessage and SMTPConnection classes The EmailMessage and SMTPConnection classes
=========================================== ===========================================


Django's `send_mail()` and `send_mass_mail()` functions are actually thin Django's ``send_mail()`` and ``send_mass_mail()`` functions are actually thin
wrappers that make use of the `EmailMessage` and `SMTPConnection` classes in wrappers that make use of the ``EmailMessage`` and ``SMTPConnection`` classes
`django.mail`. If you ever need to customize the way Django sends email, you in ``django.mail``. If you ever need to customize the way Django sends email,
can subclass these two classes to suit your needs. you can subclass these two classes to suit your needs.


.. note:: .. note::
Not all features of the `EmailMessage` class are available through the Not all features of the ``EmailMessage`` class are available through the
`send_mail()` and related wrapper functions. If you wish to use advanced ``send_mail()`` and related wrapper functions. If you wish to use advanced
features such as including BCC recipients or multi-part email, you will features such as including BCC recipients or multi-part email, you will
need to create `EmailMessage` instances directly. need to create ``EmailMessage`` instances directly.


In general, `EmailMessage` is responsible for creating the email message In general, ``EmailMessage`` is responsible for creating the email message
itself. `SMTPConnection` is responsible for the network connection side of the itself. ``SMTPConnection`` is responsible for the network connection side of
operation. This means you can reuse the same connection (an `SMTPConnection` the operation. This means you can reuse the same connection (an
instance) for multiple messages. ``SMTPConnection`` instance) for multiple messages.


The `EmailMessage` class has the following methods that you can use: The ``EmailMessage`` class is initialised as follows::


* `send()` sends the message, using either the connection that is specified email = EmailMessage(subject, body, from_email, to, bcc, connection)
in the `connection` attribute, or creating a new connection if none already
exists. All of these parameters are optional. If ``from_email`` is omitted, the value
* `message()` constructs a `django.core.mail.SafeMIMEText` object (a from ``settings.DEFAULT_FROM_EMAIL`` is used. Both the ``to`` and ``bcc``
sub-class of Python's `email.MIMEText.MIMEText` class) holding the message parameters are lists of addresses.
to be sent. If you ever need to extend the `EmailMessage` class, you will
probably want to override this method to put the content you wish into the
MIME object.


The `SMTPConnection` class is initialized with the host, port, username and The class has the following methods that you can use:

* ``send()`` sends the message, using either the connection that is specified
in the ``connection`` attribute, or creating a new connection if none already
exists.
* ``message()`` constructs a ``django.core.mail.SafeMIMEText`` object (a
sub-class of Python's ``email.MIMEText.MIMEText`` class) holding the
message to be sent. If you ever need to extend the `EmailMessage` class,
you will probably want to override this method to put the content you wish
into the MIME object.
* ``recipients()`` returns a lists of all the recipients of the message,
whether they are recorded in the ``to`` or ``bcc`` attributes. This is
another method you need to possibly override when sub-classing, since the
SMTP server needs to be told the full list of recipients when the message
is sent. If you add another way to specify recipients in your class, they
need to be returned from this method as well.

The ``SMTPConnection`` class is initialized with the host, port, username and
password for the SMTP server. If you don't specify one or more of those password for the SMTP server. If you don't specify one or more of those
options, they are read from your settings file. options, they are read from your settings file.


If you are sending lots of messages at once, the `send_messages()` method of If you are sending lots of messages at once, the ``send_messages()`` method of
the `SMTPConnection` class will be useful. It takes a list of `EmailMessage` the ``SMTPConnection`` class will be useful. It takes a list of ``EmailMessage``
instances (or sub-classes) and sends them over a single connection. For instances (or sub-classes) and sends them over a single connection. For
example, if you have a function called `get_notification_email()` that returns a example, if you have a function called ``get_notification_email()`` that returns a
list of `EmailMessage` objects representing some periodic email you wish to list of ``EmailMessage`` objects representing some periodic email you wish to
send out, you could send this with:: send out, you could send this with::


connection = SMTPConnection() # Use default settings for connection connection = SMTPConnection() # Use default settings for connection
Expand Down

0 comments on commit 5ff1d96

Please sign in to comment.