| 201 | | features, such as BCC'ed recipients or multi-part e-mail, you'll need to |
|---|
| 202 | | create ``EmailMessage`` instances directly. |
|---|
| | 201 | features, such as BCC'ed recipients, file attachments, or multi-part |
|---|
| | 202 | e-mail, you'll need to create ``EmailMessage`` instances directly. |
|---|
| | 203 | |
|---|
| | 204 | This is a design feature. ``send_mail()`` and related functions were |
|---|
| | 205 | originally the only interface Django provided. However, the list of |
|---|
| | 206 | parameters they accepted was slowly growing over time. It made sense to |
|---|
| | 207 | move to a more object-oriented design for e-mail messages and retain the |
|---|
| | 208 | original functions only for backwards compatibility. |
|---|
| 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. |
|---|
| | 215 | E-mail messages |
|---|
| | 216 | --------------- |
|---|
| | 217 | |
|---|
| | 218 | The ``EmailMessage`` class is initialized with the following parameters (in |
|---|
| | 219 | the given order, if positional arguments are used). All parameters are |
|---|
| | 220 | optional and can be set at any time prior to calling the ``send()`` method. |
|---|
| | 221 | |
|---|
| | 222 | * ``subject``: The subject line of the e-mail. |
|---|
| | 223 | |
|---|
| | 224 | * ``body``: The body text. This should be a plain text message. |
|---|
| | 225 | |
|---|
| | 226 | * ``from_email``: The sender's address. Both ``fred@example.com`` and |
|---|
| | 227 | ``Fred <fred@example.com>`` forms are legal. If omitted, the |
|---|
| | 228 | ``DEFAULT_FROM_EMAIL`` setting is used. |
|---|
| | 229 | |
|---|
| | 230 | * ``to``: A list or tuple of recipient addresses. |
|---|
| | 231 | |
|---|
| | 232 | * ``bcc``: A list or tuple of addresses used in the "Bcc" header when |
|---|
| | 233 | sending the e-mail. |
|---|
| | 234 | |
|---|
| | 235 | * ``connection``: An ``SMTPConnection`` instance. Use this parameter if |
|---|
| | 236 | you want to use the same conneciton for multiple messages. If omitted, a |
|---|
| | 237 | new connection is created when ``send()`` is called. |
|---|
| | 238 | |
|---|
| | 239 | * ``attachments``: A list of attachments to put on the message. These can |
|---|
| | 240 | be either ``email.MIMEBase.MIMEBase`` instances, or ``(filename, |
|---|
| | 241 | content, mimetype)`` triples. |
|---|
| | 242 | |
|---|
| | 243 | * ``headers``: A dictionary of extra headers to put on the message. The |
|---|
| | 244 | keys are the header name, values are the header values. It's up to the |
|---|
| | 245 | caller to ensure header names and values are in the correct format for |
|---|
| | 246 | an e-mail message. |
|---|
| | 273 | |
|---|
| | 274 | * ``attach()`` creates a new file attachment and adds it to the message. |
|---|
| | 275 | There are two ways to call ``attach()``: |
|---|
| | 276 | |
|---|
| | 277 | * You can pass it a single argument that is an |
|---|
| | 278 | ``email.MIMBase.MIMEBase`` instance. This will be inserted directly |
|---|
| | 279 | into the resulting message. |
|---|
| | 280 | |
|---|
| | 281 | * Alternatively, you can pass ``attach()`` three arguments: |
|---|
| | 282 | ``filename``, ``content`` and ``mimetype``. ``filename`` is the name |
|---|
| | 283 | of the file attachment as it will appear in the e-mail, ``content`` is |
|---|
| | 284 | the data that will be contained inside the attachment and |
|---|
| | 285 | ``mimetype`` is the optional MIME type for the attachment. If you |
|---|
| | 286 | omit ``mimetype``, the MIME content type will be guessed from the |
|---|
| | 287 | filename of the attachment. |
|---|
| | 288 | |
|---|
| | 289 | For example:: |
|---|
| | 290 | |
|---|
| | 291 | message.attach('design.png', img_data, 'image/png') |
|---|
| | 292 | |
|---|
| | 293 | * ``attach_file()`` creates a new attachment using a file from your |
|---|
| | 294 | filesystem. Call it with the path of the file to attach and, optionally, |
|---|
| | 295 | the MIME type to use for the attachment. If the MIME type is omitted, it |
|---|
| | 296 | will be guessed from the filename. The simplest use would be:: |
|---|
| | 297 | |
|---|
| | 298 | message.attach_file('/images/weather_map.png') |
|---|
| | 299 | |
|---|
| | 300 | Sending alternative content types |
|---|
| | 301 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 302 | |
|---|
| | 303 | It can be useful to include multiple versions of the content in an e-mail; |
|---|
| | 304 | the classic example is to send both text and HTML versions of a message. With |
|---|
| | 305 | Django's e-mail library, you can do this using the ``EmailMultiAlternatives`` |
|---|
| | 306 | class. This subclass of ``EmailMessage`` has an ``attach_alternative()`` method |
|---|
| | 307 | for including extra versions of the message body in the e-mail. All the other |
|---|
| | 308 | methods (including the class initialization) are inherited directly from |
|---|
| | 309 | ``EmailMessage``. |
|---|
| | 310 | |
|---|
| | 311 | To send a text and HTML combination, you could write:: |
|---|
| | 312 | |
|---|
| | 313 | from django.core.mail import EmailMultiAlternatives |
|---|
| | 314 | |
|---|
| | 315 | subject, from_email, to = 'hello', 'from@example.com', 'to@example.com' |
|---|
| | 316 | text_content = 'This is an important message.' |
|---|
| | 317 | html_content = '<p>This is an <strong>important</strong> message.' |
|---|
| | 318 | msg = EmailMultiAlternatives(subject, text_content, from_email, to) |
|---|
| | 319 | msg.attach_alternative(html_content, "text/html") |
|---|
| | 320 | msg.send() |
|---|
| | 321 | |
|---|
| | 322 | By default, the MIME type of the ``body`` parameter in an ``EmailMessage`` is |
|---|
| | 323 | ``"text/plain"``. It is good practice to leave this alone, because it |
|---|
| | 324 | guarantees that any recipient will be able to read the e-mail, regardless of |
|---|
| | 325 | their mail client. However, if you are confident that your recipients can |
|---|
| | 326 | handle an alternative content type, you can use the ``content_subtype`` |
|---|
| | 327 | attribute on the ``EmailMessage`` class to change the main content type. The |
|---|
| | 328 | major type will always be ``"text"``, but you can change it to the subtype. For |
|---|
| | 329 | example:: |
|---|
| | 330 | |
|---|
| | 331 | msg = EmailMessage(subject, html_content, from_email, to) |
|---|
| | 332 | msg.content_subtype = "html" # Main content is now text/html |
|---|
| | 333 | msg.send() |
|---|
| | 334 | |
|---|
| | 335 | SMTP network connections |
|---|
| | 336 | ------------------------ |
|---|