Edited docs/email.txt changes from [5141] and other recent changesets

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5155 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2007-05-06 04:23:12 +00:00
parent 1889db475d
commit 64adc41377
1 changed files with 40 additions and 34 deletions

View File

@ -20,14 +20,14 @@ In two lines::
send_mail('Subject here', 'Here is the message.', 'from@example.com', send_mail('Subject here', 'Here is the message.', 'from@example.com',
['to@example.com'], fail_silently=False) ['to@example.com'], fail_silently=False)
Mail will be sent using the SMTP host and port specified in the `EMAIL_HOST`_ Mail is sent using the SMTP host and port specified in the `EMAIL_HOST`_ and
and `EMAIL_PORT`_ settings. The `EMAIL_HOST_USER`_ and `EMAIL_HOST_PASSWORD`_ `EMAIL_PORT`_ settings. The `EMAIL_HOST_USER`_ and `EMAIL_HOST_PASSWORD`_
settings, if set, will be used to authenticate to the SMTP server and the settings, if set, are used to authenticate to the SMTP server, and the
`EMAIL_USE_TLS`_ settings will control whether a secure connection is used. `EMAIL_USE_TLS`_ setting controls whether a secure connection is used.
.. note:: .. note::
The character set of email sent with ``django.core.mail`` will be set to The character set of e-mail sent with ``django.core.mail`` will be set to
the value of your `DEFAULT_CHARSET setting`_. the value of your `DEFAULT_CHARSET setting`_.
.. _DEFAULT_CHARSET setting: ../settings/#default-charset .. _DEFAULT_CHARSET setting: ../settings/#default-charset
@ -37,7 +37,6 @@ settings, if set, will be used to authenticate to the SMTP server and the
.. _EMAIL_HOST_PASSWORD: ../settings/#email-host-password .. _EMAIL_HOST_PASSWORD: ../settings/#email-host-password
.. _EMAIL_USE_TLS: ../settings/#email-use-tls .. _EMAIL_USE_TLS: ../settings/#email-use-tls
send_mail() send_mail()
=========== ===========
@ -193,57 +192,64 @@ 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 wrappers that make use of the ``EmailMessage`` and ``SMTPConnection`` classes
in ``django.mail``. If you ever need to customize the way Django sends email, in ``django.core.mail``. If you ever need to customize the way Django sends
you can subclass these two classes to suit your needs. e-mail, 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 BCC'ed recipients or multi-part e-mail, you'll need to
need to create ``EmailMessage`` instances directly. create ``EmailMessage`` instances directly.
In general, ``EmailMessage`` is responsible for creating the email message In general, ``EmailMessage`` is responsible for creating the e-mail message
itself. ``SMTPConnection`` is responsible for the network connection side of itself. ``SMTPConnection`` is responsible for the network connection side of
the operation. This means you can reuse the same connection (an the operation. This means you can reuse the same connection (an
``SMTPConnection`` instance) for multiple messages. ``SMTPConnection`` instance) for multiple messages.
The ``EmailMessage`` class is initialised as follows:: The ``EmailMessage`` class is initialized as follows::
email = EmailMessage(subject, body, from_email, to, bcc, connection) email = EmailMessage(subject, body, from_email, to, bcc, connection)
All of these parameters are optional. If ``from_email`` is omitted, the value All of these parameters are optional. If ``from_email`` is omitted, the value
from ``settings.DEFAULT_FROM_EMAIL`` is used. Both the ``to`` and ``bcc`` from ``settings.DEFAULT_FROM_EMAIL`` is used. Both the ``to`` and ``bcc``
parameters are lists of addresses. parameters are lists of addresses, as strings.
The class has the following methods that you can use: For example::
* ``send()`` sends the message, using either the connection that is specified email = EmailMessage('Hello', 'Body goes here', 'from@example.com',
in the ``connection`` attribute, or creating a new connection if none already ['to1@example.com', 'to2@example.com'],
exists. ['bcc@example.com'])
* ``message()`` constructs a ``django.core.mail.SafeMIMEText`` object (a
sub-class of Python's ``email.MIMEText.MIMEText`` class) holding the The class has the following methods:
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 * ``send()`` sends the message, using either the connection that is
into the MIME object. specified in the ``connection`` attribute, or creating a new connection
* ``recipients()`` returns a lists of all the recipients of the message, if none already exists.
whether they are recorded in the ``to`` or ``bcc`` attributes. This is
another method you need to possibly override when sub-classing, since the * ``message()`` constructs a ``django.core.mail.SafeMIMEText`` object (a
SMTP server needs to be told the full list of recipients when the message sub-class of Python's ``email.MIMEText.MIMEText`` class) holding the
is sent. If you add another way to specify recipients in your class, they message to be sent. If you ever need to extend the `EmailMessage` class,
need to be returned from this method as well. you'll probably want to override this method to put the content you wish
into the MIME object.
* ``recipients()`` returns a list of all the recipients of the message,
whether they're recorded in the ``to`` or ``bcc`` attributes. This is
another method you might need to override when sub-classing, because 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 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're 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 is useful. It takes a list of ``EmailMessage``
instances (or sub-classes) and sends them over a single connection. For instances (or subclasses) and sends them over a single connection. For example,
example, if you have a function called ``get_notification_email()`` that returns a 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 e-mail 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
messages = get_notification_email() messages = get_notification_email()
connection.send_messages(messages) connection.send_messages(messages)