Django

Code

Show
Ignore:
Timestamp:
06/17/07 17:18:54 (2 years ago)
Author:
clong
Message:

per-object-permissions: Merged to trunk [5486] NOTE: Not fully tested, will be working on this over the next few weeks.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/per-object-permissions/docs/email.txt

    r2901 r5488  
    2020    send_mail('Subject here', 'Here is the message.', 'from@example.com', 
    2121        ['to@example.com'], fail_silently=False) 
    22          
     22 
     23Mail is sent using the SMTP host and port specified in the `EMAIL_HOST`_ and 
     24`EMAIL_PORT`_ settings. The `EMAIL_HOST_USER`_ and `EMAIL_HOST_PASSWORD`_ 
     25settings, if set, are used to authenticate to the SMTP server, and the 
     26`EMAIL_USE_TLS`_ setting controls whether a secure connection is used. 
     27 
    2328.. note:: 
    2429 
    25     The character set of email sent with ``django.core.mail`` will be set to 
     30    The character set of e-mail sent with ``django.core.mail`` will be set to 
    2631    the value of your `DEFAULT_CHARSET setting`_. 
    27      
    28 .. _DEFAULT_CHARSET setting: ../settings/#DEFAULT_CHARSET 
     32 
     33.. _DEFAULT_CHARSET setting: ../settings/#default-charset 
     34.. _EMAIL_HOST: ../settings/#email-host 
     35.. _EMAIL_PORT: ../settings/#email-port 
     36.. _EMAIL_HOST_USER: ../settings/#email-host-user 
     37.. _EMAIL_HOST_PASSWORD: ../settings/#email-host-password 
     38.. _EMAIL_USE_TLS: ../settings/#email-use-tls 
    2939 
    3040send_mail() 
     
    3545 
    3646    send_mail(subject, message, from_email, recipient_list, 
    37         fail_silently=False, auth_user=EMAIL_HOST_USER
    38         auth_password=EMAIL_HOST_PASSWORD
     47        fail_silently=False, auth_user=None
     48        auth_password=None
    3949 
    4050The ``subject``, ``message``, ``from_email`` and ``recipient_list`` parameters 
     
    6676 
    6777    send_mass_mail(datatuple, fail_silently=False, 
    68         auth_user=EMAIL_HOST_USER, auth_password=EMAIL_HOST_PASSWORD): 
     78        auth_user=None, auth_password=None): 
    6979 
    7080``datatuple`` is a tuple in which each element is in this format:: 
     
    102112This method exists for convenience and readability. 
    103113 
    104 .. _ADMINS setting: http://www.djangoproject.com/documentation/settings/#admins 
    105 .. _EMAIL_SUBJECT_PREFIX setting: http://www.djangoproject.com/documentation/settings/#email-subject-prefix 
    106 .. _SERVER_EMAIL setting: http://www.djangoproject.com/documentation/settings/#server-email 
     114.. _ADMINS setting: ../settings/#admins 
     115.. _EMAIL_SUBJECT_PREFIX setting: ../settings/#email-subject-prefix 
     116.. _SERVER_EMAIL setting: ../settings/#server-email 
    107117 
    108118mail_managers() function 
     
    115125    mail_managers(subject, message, fail_silently=False) 
    116126 
    117 .. _MANAGERS setting: http://www.djangoproject.com/documentation/settings/#managers 
     127.. _MANAGERS setting: ../settings/#managers 
    118128 
    119129Examples 
     
    175185 
    176186.. _Header injection: http://securephp.damonkohler.com/index.php/Email_Injection 
     187 
     188The EmailMessage and SMTPConnection classes 
     189=========================================== 
     190 
     191**New in Django development version** 
     192 
     193Django's ``send_mail()`` and ``send_mass_mail()`` functions are actually thin 
     194wrappers that make use of the ``EmailMessage`` and ``SMTPConnection`` classes 
     195in ``django.core.mail``.  If you ever need to customize the way Django sends 
     196e-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 
     204In general, ``EmailMessage`` is responsible for creating the e-mail message 
     205itself. ``SMTPConnection`` is responsible for the network connection side of 
     206the operation. This means you can reuse the same connection (an 
     207``SMTPConnection`` instance) for multiple messages. 
     208 
     209The ``EmailMessage`` class is initialized as follows:: 
     210 
     211    email = EmailMessage(subject, body, from_email, to, bcc, connection) 
     212 
     213All of these parameters are optional. If ``from_email`` is omitted, the value 
     214from ``settings.DEFAULT_FROM_EMAIL`` is used. Both the ``to`` and ``bcc`` 
     215parameters are lists of addresses, as strings. 
     216 
     217For example:: 
     218 
     219    email = EmailMessage('Hello', 'Body goes here', 'from@example.com', 
     220                ['to1@example.com', 'to2@example.com'], 
     221                ['bcc@example.com']) 
     222 
     223The 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 
     242The ``SMTPConnection`` class is initialized with the host, port, username and 
     243password for the SMTP server. If you don't specify one or more of those 
     244options, they are read from your settings file. 
     245 
     246If you're sending lots of messages at once, the ``send_messages()`` method of 
     247the ``SMTPConnection`` class is useful. It takes a list of ``EmailMessage`` 
     248instances (or subclasses) and sends them over a single connection. For example, 
     249if you have a function called ``get_notification_email()`` that returns a 
     250list of ``EmailMessage`` objects representing some periodic e-mail you wish to 
     251send 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)