| | 187 | |
|---|
| | 188 | The EmailMessage and SMTPConnection classes |
|---|
| | 189 | =========================================== |
|---|
| | 190 | |
|---|
| | 191 | **New in Django development version** |
|---|
| | 192 | |
|---|
| | 193 | Django's ``send_mail()`` and ``send_mass_mail()`` functions are actually thin |
|---|
| | 194 | wrappers that make use of the ``EmailMessage`` and ``SMTPConnection`` classes |
|---|
| | 195 | in ``django.core.mail``. If you ever need to customize the way Django sends |
|---|
| | 196 | e-mail, you can subclass these two classes to suit your needs. |
|---|
| | 197 | |
|---|
| | 198 | .. note:: |
|---|
| | 199 | Not all features of the ``EmailMessage`` class are available through the |
|---|
| | 200 | ``send_mail()`` and related wrapper functions. If you wish to use advanced |
|---|
| | 201 | features, such as BCC'ed recipients or multi-part e-mail, you'll need to |
|---|
| | 202 | create ``EmailMessage`` instances directly. |
|---|
| | 203 | |
|---|
| | 204 | In general, ``EmailMessage`` is responsible for creating the e-mail message |
|---|
| | 205 | itself. ``SMTPConnection`` is responsible for the network connection side of |
|---|
| | 206 | the operation. This means you can reuse the same connection (an |
|---|
| | 207 | ``SMTPConnection`` instance) for multiple messages. |
|---|
| | 208 | |
|---|
| | 209 | The ``EmailMessage`` class is initialized as follows:: |
|---|
| | 210 | |
|---|
| | 211 | email = EmailMessage(subject, body, from_email, to, bcc, connection) |
|---|
| | 212 | |
|---|
| | 213 | All of these parameters are optional. If ``from_email`` is omitted, the value |
|---|
| | 214 | from ``settings.DEFAULT_FROM_EMAIL`` is used. Both the ``to`` and ``bcc`` |
|---|
| | 215 | parameters are lists of addresses, as strings. |
|---|
| | 216 | |
|---|
| | 217 | For example:: |
|---|
| | 218 | |
|---|
| | 219 | email = EmailMessage('Hello', 'Body goes here', 'from@example.com', |
|---|
| | 220 | ['to1@example.com', 'to2@example.com'], |
|---|
| | 221 | ['bcc@example.com']) |
|---|
| | 222 | |
|---|
| | 223 | The class has the following methods: |
|---|
| | 224 | |
|---|
| | 225 | * ``send()`` sends the message, using either the connection that is |
|---|
| | 226 | specified in the ``connection`` attribute, or creating a new connection |
|---|
| | 227 | if none already exists. |
|---|
| | 228 | |
|---|
| | 229 | * ``message()`` constructs a ``django.core.mail.SafeMIMEText`` object (a |
|---|
| | 230 | sub-class of Python's ``email.MIMEText.MIMEText`` class) holding the |
|---|
| | 231 | message to be sent. If you ever need to extend the `EmailMessage` class, |
|---|
| | 232 | you'll probably want to override this method to put the content you wish |
|---|
| | 233 | into the MIME object. |
|---|
| | 234 | |
|---|
| | 235 | * ``recipients()`` returns a list of all the recipients of the message, |
|---|
| | 236 | whether they're recorded in the ``to`` or ``bcc`` attributes. This is |
|---|
| | 237 | another method you might need to override when sub-classing, because the |
|---|
| | 238 | SMTP server needs to be told the full list of recipients when the message |
|---|
| | 239 | is sent. If you add another way to specify recipients in your class, they |
|---|
| | 240 | need to be returned from this method as well. |
|---|
| | 241 | |
|---|
| | 242 | The ``SMTPConnection`` class is initialized with the host, port, username and |
|---|
| | 243 | password for the SMTP server. If you don't specify one or more of those |
|---|
| | 244 | options, they are read from your settings file. |
|---|
| | 245 | |
|---|
| | 246 | If you're sending lots of messages at once, the ``send_messages()`` method of |
|---|
| | 247 | the ``SMTPConnection`` class is useful. It takes a list of ``EmailMessage`` |
|---|
| | 248 | instances (or subclasses) and sends them over a single connection. For example, |
|---|
| | 249 | if you have a function called ``get_notification_email()`` that returns a |
|---|
| | 250 | list of ``EmailMessage`` objects representing some periodic e-mail you wish to |
|---|
| | 251 | send out, you could send this with:: |
|---|
| | 252 | |
|---|
| | 253 | connection = SMTPConnection() # Use default settings for connection |
|---|
| | 254 | messages = get_notification_email() |
|---|
| | 255 | connection.send_messages(messages) |
|---|