@@ -313,7 +313,7 @@ recipients, file attachments, or multi-part email, you'll need to create
313313message itself. The :ref:`email backend <topic-email-backends>` is then
314314responsible for sending the email.
315315
316- For convenience, :class:`~django.core.mail. EmailMessage` provides a `` send()` `
316+ For convenience, :class:`EmailMessage` provides a :meth:`~EmailMessage. send`
317317method for sending a single email. If you need to send multiple messages, the
318318email backend API :ref:`provides an alternative
319319<topics-sending-multiple-emails>`.
@@ -323,185 +323,201 @@ email backend API :ref:`provides an alternative
323323
324324.. class:: EmailMessage
325325
326- The :class:`~django.core.mail. EmailMessage` class is initialized with the
327- following parameters. All parameters are optional and can be set at any time
328- prior to calling the `` send()` ` method.
326+ The :class:`! EmailMessage` class is initialized with the following
327+ parameters. All parameters are optional and can be set at any time prior
328+ to calling the :meth:` send` method.
329329
330- The first four parameters can be passed as positional or keyword arguments,
331- but must be in the given order if positional arguments are used:
330+ The first four parameters can be passed as positional or keyword arguments,
331+ but must be in the given order if positional arguments are used:
332332
333- * ``subject``: The subject line of the email.
333+ * ``subject``: The subject line of the email.
334334
335- * ``body``: The body text. This should be a plain text message.
335+ * ``body``: The body text. This should be a plain text message.
336336
337- * ``from_email``: The sender's address. Both ``fred@example.com`` and
338- ``"Fred" <fred@example.com>`` forms are legal. If omitted, the
339- :setting:`DEFAULT_FROM_EMAIL` setting is used.
337+ * ``from_email``: The sender's address. Both ``fred@example.com`` and
338+ ``"Fred" <fred@example.com>`` forms are legal. If omitted, the
339+ :setting:`DEFAULT_FROM_EMAIL` setting is used.
340340
341- * ``to``: A list or tuple of recipient addresses.
341+ * ``to``: A list or tuple of recipient addresses.
342342
343- The following parameters must be given as keyword arguments if used:
343+ The following parameters must be given as keyword arguments if used:
344344
345- * ``cc``: A list or tuple of recipient addresses used in the "Cc" header
346- when sending the email.
345+ * ``cc``: A list or tuple of recipient addresses used in the "Cc" header
346+ when sending the email.
347347
348- * ``bcc``: A list or tuple of addresses used in the "Bcc" header when
349- sending the email.
348+ * ``bcc``: A list or tuple of addresses used in the "Bcc" header when
349+ sending the email.
350350
351- * ``reply_to``: A list or tuple of recipient addresses used in the "Reply-To"
352- header when sending the email.
351+ * ``reply_to``: A list or tuple of recipient addresses used in the
352+ "Reply-To" header when sending the email.
353353
354- * ``attachments``: A list of attachments to put on the message. Each can
355- be an instance of :class:`~email.message.MIMEPart` or
356- :class:`~django.core.mail. EmailAttachment`, or a tuple with attributes
357- ``(filename, content, mimetype)``.
354+ * ``attachments``: A list of attachments to put on the message. Each can
355+ be an instance of :class:`~email.message.MIMEPart` or
356+ :class:`EmailAttachment`, or a tuple with attributes
357+ ``(filename, content, mimetype)``.
358358
359- .. versionchanged:: 5.2
359+ .. versionchanged:: 5.2
360360
361- Support for :class:`~django.core.mail. EmailAttachment` items of
362- ``attachments`` was added.
361+ Support for :class:`EmailAttachment` items of ``attachments`` was
362+ added.
363363
364- .. versionchanged:: 6.0
364+ .. versionchanged:: 6.0
365365
366- Support for :class:`~email.message.MIMEPart` objects in the ``attachments``
367- list was added.
366+ Support for :class:`~email.message.MIMEPart` objects in the
367+ ``attachments`` list was added.
368368
369- .. deprecated:: 6.0
369+ .. deprecated:: 6.0
370370
371- Support for Python's legacy :class:`~email.mime.base.MIMEBase` objects in
372- ``attachments`` is deprecated. Use :class:`~email.message.MIMEPart`
373- instead.
371+ Support for Python's legacy :class:`~email.mime.base.MIMEBase`
372+ objects in ``attachments`` is deprecated. Use
373+ :class:`~email.message.MIMEPart` instead.
374374
375- * ``headers``: A dictionary of extra headers to put on the message. The
376- keys are the header name, values are the header values. It's up to the
377- caller to ensure header names and values are in the correct format for
378- an email message. The corresponding attribute is ``extra_headers``.
375+ * ``headers``: A dictionary of extra headers to put on the message. The
376+ keys are the header name, values are the header values. It's up to the
377+ caller to ensure header names and values are in the correct format for
378+ an email message. The corresponding attribute is ``extra_headers``.
379379
380- * ``connection``: An :ref:`email backend <topic-email-backends>` instance. Use
381- this parameter if you are sending the ``EmailMessage`` via ``send()`` and you
382- want to use the same connection for multiple messages. If omitted, a new
383- connection is created when ``send()`` is called. This parameter is ignored
384- when using :ref:`send_messages() <topics-sending-multiple-emails>`.
380+ * ``connection``: An :ref:`email backend <topic-email-backends>` instance.
381+ Use this parameter if you are sending the :class:`!EmailMessage` via
382+ :meth:`send` and you want to use the same connection for multiple
383+ messages. If omitted, a new connection is created when :meth:`send` is
384+ called. This parameter is ignored when using
385+ :ref:`send_messages() <topics-sending-multiple-emails>`.
385386
386- .. deprecated:: 6.0
387+ .. deprecated:: 6.0
387388
388- Passing all except the first four parameters as positional arguments is
389- deprecated.
389+ Passing all except the first four parameters as positional arguments is
390+ deprecated.
390391
391- For example::
392+ For example::
392393
393- from django.core.mail import EmailMessage
394+ from django.core.mail import EmailMessage
394395
395- email = EmailMessage(
396- subject="Hello",
397- body="Body goes here",
398- from_email="from@example.com",
399- to=["to1@example.com", "to2@example.com"],
400- bcc=["bcc@example.com"],
401- reply_to=["another@example.com"],
402- headers={"Message-ID": "foo"},
403- )
396+ email = EmailMessage(
397+ subject="Hello",
398+ body="Body goes here",
399+ from_email="from@example.com",
400+ to=["to1@example.com", "to2@example.com"],
401+ bcc=["bcc@example.com"],
402+ reply_to=["another@example.com"],
403+ headers={"Message-ID": "foo"},
404+ )
404405
405- The class has the following methods:
406-
407- * ``send(fail_silently=False)`` sends the message. If a connection was
408- specified when the email was constructed, that connection will be used.
409- Otherwise, an instance of the default backend will be instantiated and
410- used. If the keyword argument ``fail_silently`` is ``True``, exceptions
411- raised while sending the message will be quashed. An empty list of
412- recipients will not raise an exception. It will return ``1`` if the message
413- was sent successfully, otherwise ``0``.
414-
415- * ``message(policy=email.policy.default)`` constructs and returns a Python
416- :class:`email.message.EmailMessage` object representing the message to be
417- sent.
418-
419- The keyword argument ``policy`` allows specifying the set of rules for
420- updating and serializing the representation of the message. It must be an
421- :mod:`email.policy.Policy <email.policy>` object. Defaults to
422- :data:`email.policy.default`. In certain cases you may want to use
423- :data:`~email.policy.SMTP`, :data:`~email.policy.SMTPUTF8` or a custom
424- policy. For example, :class:`django.core.mail.backends.smtp.EmailBackend`
425- uses the :data:`~email.policy.SMTP` policy to ensure ``\r\n`` line endings
426- as required by the SMTP protocol.
427-
428- If you ever need to extend Django's :class:`~django.core.mail.EmailMessage`
429- class, you'll probably want to override this method to put the content you
430- want into the Python EmailMessage object.
431-
432- .. versionchanged:: 6.0
433-
434- The ``policy`` keyword argument was added and the return type was updated
435- to an instance of :class:`~email.message.EmailMessage`.
436-
437- * ``recipients()`` returns a list of all the recipients of the message,
438- whether they're recorded in the ``to``, ``cc`` or ``bcc`` attributes. This
439- is another method you might need to override when subclassing, because the
440- SMTP server needs to be told the full list of recipients when the message
441- is sent. If you add another way to specify recipients in your class, they
442- need to be returned from this method as well.
443-
444- * ``attach()`` creates a new attachment and adds it to the message.
445- There are two ways to call ``attach()``:
446-
447- * You can pass it three arguments: ``filename``, ``content`` and
448- ``mimetype``. ``filename`` is the name of the file attachment as it will
449- appear in the email, ``content`` is the data that will be contained inside
450- the attachment and ``mimetype`` is the optional MIME type for the
451- attachment. If you omit ``mimetype``, the MIME content type will be guessed
452- from the filename of the attachment.
406+ The class has the following methods:
453407
454- For example::
408+ .. method:: send(fail_silently=False)
455409
456- message.attach("design.png", img_data, "image/png")
457-
458- If you specify a ``mimetype`` of :mimetype:`message/rfc822`, ``content``
459- can be a :class:`django.core.mail.EmailMessage` or Python's
460- :class:`email.message.EmailMessage` or :class:`email.message.Message`.
461-
462- For a ``mimetype`` starting with :mimetype:`text/`, content is expected to
463- be a string. Binary data will be decoded using UTF-8, and if that fails,
464- the MIME type will be changed to :mimetype:`application/octet-stream` and
465- the data will be attached unchanged.
466-
467- * Or for attachments requiring additional headers or parameters, you can pass
468- ``attach()`` a single Python :class:`~email.message.MIMEPart` object.
469- This will be attached directly to the resulting message. For example,
470- to attach an inline image with a :mailheader:`Content-ID`::
471-
472- cid = email.utils.make_msgid()
473- inline_image = email.message.MIMEPart()
474- inline_image.set_content(
475- image_data_bytes,
476- maintype="image",
477- subtype="png",
478- disposition="inline",
479- cid=f"<{cid}>",
480- )
481- message.attach(inline_image)
482- message.attach_alternative(f'… <img src="cid:${cid}"> …', "text/html")
410+ Sends the message. If a connection was specified when the email was
411+ constructed, that connection will be used. Otherwise, an instance of
412+ the default backend will be instantiated and used. If the keyword
413+ argument ``fail_silently`` is ``True``, exceptions raised while sending
414+ the message will be quashed. An empty list of recipients will not raise
415+ an exception. It will return ``1`` if the message was sent
416+ successfully, otherwise ``0``.
483417
484- Python's :meth:`email.contentmanager.set_content` documentation describes
485- the supported arguments for ``MIMEPart.set_content()``.
418+ .. method:: message(policy=email.policy.default)
486419
487- .. versionchanged:: 6.0
420+ Constructs and returns a Python :class:`email.message.EmailMessage`
421+ object representing the message to be sent.
488422
489- Support for :class:`~email.message.MIMEPart` attachments was added.
423+ The keyword argument ``policy`` allows specifying the set of rules for
424+ updating and serializing the representation of the message. It must be
425+ an :mod:`email.policy.Policy <email.policy>` object. Defaults to
426+ :data:`email.policy.default`. In certain cases you may want to use
427+ :data:`~email.policy.SMTP`, :data:`~email.policy.SMTPUTF8` or a custom
428+ policy. For example,
429+ :class:`django.core.mail.backends.smtp.EmailBackend` uses the
430+ :data:`~email.policy.SMTP` policy to ensure ``\r\n`` line endings as
431+ required by the SMTP protocol.
490432
491- .. deprecated:: 6.0
433+ If you ever need to extend Django's :class:`EmailMessage` class,
434+ you'll probably want to override this method to put the content you
435+ want into the Python EmailMessage object.
436+
437+ .. versionchanged:: 6.0
438+
439+ The ``policy`` keyword argument was added and the return type was
440+ updated to an instance of :class:`~email.message.EmailMessage`.
441+
442+ .. method:: recipients()
443+
444+ Returns a list of all the recipients of the message, whether they're
445+ recorded in the ``to``, ``cc`` or ``bcc`` attributes. This is another
446+ method you might need to override when subclassing, because the SMTP
447+ server needs to be told the full list of recipients when the message
448+ is sent. If you add another way to specify recipients in your class,
449+ they need to be returned from this method as well.
450+
451+ .. method:: attach(filename, content, mimetype)
452+ attach(mimepart)
453+
454+ Creates a new attachment and adds it to the message. There are two ways
455+ to call :meth:`!attach`:
456+
457+ * You can pass it three arguments: ``filename``, ``content`` and
458+ ``mimetype``. ``filename`` is the name of the file attachment as it
459+ will appear in the email, ``content`` is the data that will be
460+ contained inside the attachment and ``mimetype`` is the optional MIME
461+ type for the attachment. If you omit ``mimetype``, the MIME content
462+ type will be guessed from the filename of the attachment.
463+
464+ For example::
465+
466+ message.attach("design.png", img_data, "image/png")
467+
468+ If you specify a ``mimetype`` of :mimetype:`message/rfc822`,
469+ ``content`` can be a :class:`django.core.mail.EmailMessage` or
470+ Python's :class:`email.message.EmailMessage` or
471+ :class:`email.message.Message`.
472+
473+ For a ``mimetype`` starting with :mimetype:`text/`, content is
474+ expected to be a string. Binary data will be decoded using UTF-8,
475+ and if that fails, the MIME type will be changed to
476+ :mimetype:`application/octet-stream` and the data will be attached
477+ unchanged.
478+
479+ * Or for attachments requiring additional headers or parameters, you
480+ can pass :meth:`!attach` a single Python
481+ :class:`~email.message.MIMEPart` object. This will be attached
482+ directly to the resulting message. For example, to attach an inline
483+ image with a :mailheader:`Content-ID`::
484+
485+ cid = email.utils.make_msgid()
486+ inline_image = email.message.MIMEPart()
487+ inline_image.set_content(
488+ image_data_bytes,
489+ maintype="image",
490+ subtype="png",
491+ disposition="inline",
492+ cid=f"<{cid}>",
493+ )
494+ message.attach(inline_image)
495+ message.attach_alternative(f'… <img src="cid:${cid}"> …', "text/html")
496+
497+ Python's :meth:`email.contentmanager.set_content` documentation
498+ describes the supported arguments for ``MIMEPart.set_content()``.
499+
500+ .. versionchanged:: 6.0
501+
502+ Support for :class:`~email.message.MIMEPart` attachments was
503+ added.
492504
493- Support for :class:`email.mime.base.MIMEBase` attachments is
494- deprecated. Use :class:`~email.message.MIMEPart` instead.
505+ .. deprecated:: 6.0
495506
496- * ``attach_file()`` creates a new attachment using a file from your
497- filesystem. Call it with the path of the file to attach and, optionally,
498- the MIME type to use for the attachment. If the MIME type is omitted, it
499- will be guessed from the filename. You can use it like this::
507+ Support for :class:`email.mime.base.MIMEBase` attachments is
508+ deprecated. Use :class:`~email.message.MIMEPart` instead.
509+
510+ .. method:: attach_file(path, mimetype=None)
511+
512+ Creates a new attachment using a file from your filesystem. Call it
513+ with the path of the file to attach and, optionally, the MIME type to
514+ use for the attachment. If the MIME type is omitted, it will be guessed
515+ from the filename. You can use it like this::
500516
501- message.attach_file("/images/weather_map.png")
517+ message.attach_file("/images/weather_map.png")
502518
503- For MIME types starting with :mimetype:`text/`, binary data is handled as in
504- `` attach()` `.
519+ For MIME types starting with :mimetype:`text/`, binary data is handled
520+ as in :meth:` attach`.
505521
506522.. class:: EmailAttachment
507523
0 commit comments