Edited docs/email.txt changes from [5548]

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5562 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2007-06-30 21:06:47 +00:00
parent 918738919e
commit b67a2feebf
1 changed files with 16 additions and 15 deletions

View File

@ -300,32 +300,33 @@ The class has the following methods:
Sending alternative content types Sending alternative content types
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
It is often useful to include multiple versions of the content in an e-mail. It can be useful to include multiple versions of the content in an e-mail;
For instance, sending both text and HTML versions of an e-mail. You can do the classic example is to send both text and HTML versions of a message. With
this using the ``EmailMultiAlternatives`` class. This sub-class of Django's e-mail library, you can do this using the ``EmailMultiAlternatives``
``EmailMessage`` has an ``attach_alternative()`` method for including class. This subclass of ``EmailMessage`` has an ``attach_alternative()`` method
extra versions of the message body in the e-mail. All the other methods for including extra versions of the message body in the e-mail. All the other
(including the class initialization) are inherited directly from methods (including the class initialization) are inherited directly from
``EmailMessage``. ``EmailMessage``.
To send a text and HTML combination, you could write:: To send a text and HTML combination, you could write::
from django.core.mail import EmailMultiAlternatives from django.core.mail import EmailMultiAlternatives
subject, from_email, to = ... subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
text_content = "This is an important message." text_content = 'This is an important message.'
html_content = "<p>This is an <strong>important</strong> message." html_content = '<p>This is an <strong>important</strong> message.'
msg = EmailMultiAlternatives(subject, text_content, from_email, to) msg = EmailMultiAlternatives(subject, text_content, from_email, to)
msg.attach_alternative(html_content, "text/html") msg.attach_alternative(html_content, "text/html")
msg.send() msg.send()
By default, the MIME type of the ``body`` parameter in an ``EmailMessage`` is By default, the MIME type of the ``body`` parameter in an ``EmailMessage`` is
``"text/plain"``. It is good practice to leave this alone, since it guarantees ``"text/plain"``. It is good practice to leave this alone, because it
that any recipient will be able to read the e-mail, regardless of their mail guarantees that any recipient will be able to read the e-mail, regardless of
client. However, if you are confident that your recipients can handle an their mail client. However, if you are confident that your recipients can
alternative content type, you can use the ``content_subtype`` attribute on the handle an alternative content type, you can use the ``content_subtype``
``EmailMessage`` class to change the main content type. The major type will attribute on the ``EmailMessage`` class to change the main content type. The
always be ``"text"``, but you can change to the subtype. For example:: major type will always be ``"text"``, but you can change it to the subtype. For
example::
msg = EmailMessage(subject, html_content, from_email, to) msg = EmailMessage(subject, html_content, from_email, to)
msg.content_subtype = "html" # Main content is now text/html msg.content_subtype = "html" # Main content is now text/html