Django

Code

Show
Ignore:
Timestamp:
07/01/07 00:55:01 (2 years ago)
Author:
mtredinnick
Message:

unicode: Merged changes from trunk up to [5579].

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/unicode

    • Property svnmerge-integrated changed from /django/trunk:1-5530 to /django/trunk:1-5579
  • django/branches/unicode/docs/email.txt

    r5185 r5580  
    2929 
    3030    The character set of e-mail sent with ``django.core.mail`` will be set to 
    31     the value of your `DEFAULT_CHARSET setting`_
    32  
    33 .. _DEFAULT_CHARSET setting: ../settings/#default-charset 
     31    the value of your `DEFAULT_CHARSET`_ setting
     32 
     33.. _DEFAULT_CHARSET: ../settings/#default-charset 
    3434.. _EMAIL_HOST: ../settings/#email-host 
    3535.. _EMAIL_PORT: ../settings/#email-port 
     
    199199    Not all features of the ``EmailMessage`` class are available through the 
    200200    ``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. 
     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. 
    203209 
    204210In general, ``EmailMessage`` is responsible for creating the e-mail message 
     
    207213``SMTPConnection`` instance) for multiple messages. 
    208214 
    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. 
     215E-mail messages 
     216--------------- 
     217 
     218The ``EmailMessage`` class is initialized with the following parameters (in 
     219the given order, if positional arguments are used). All parameters are 
     220optional 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. 
    216247 
    217248For example:: 
    218249 
    219250    email = EmailMessage('Hello', 'Body goes here', 'from@example.com', 
    220                 ['to1@example.com', 'to2@example.com'], 
    221                 ['bcc@example.com']
     251                ['to1@example.com', 'to2@example.com'], ['bcc@example.com'], 
     252                headers = {'Reply-To': 'another@example.com'}
    222253 
    223254The class has the following methods: 
     
    228259 
    229260    * ``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 
     261      subclass of Python's ``email.MIMEText.MIMEText`` class) or a 
     262      ``django.core.mail.SafeMIMEMultipart`` object holding the 
     263      message to be sent. If you ever need to extend the ``EmailMessage`` class, 
     264      you'll probably want to override this method to put the content you want 
    233265      into the MIME object. 
    234266 
    235267    * ``recipients()`` returns a list of all the recipients of the message, 
    236268      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 
     269      another method you might need to override when subclassing, because the 
    238270      SMTP server needs to be told the full list of recipients when the message 
    239271      is sent. If you add another way to specify recipients in your class, they 
    240272      need to be returned from this method as well. 
     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 
     300Sending alternative content types 
     301~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     302 
     303It can be useful to include multiple versions of the content in an e-mail; 
     304the classic example is to send both text and HTML versions of a message. With 
     305Django's e-mail library, you can do this using the ``EmailMultiAlternatives`` 
     306class. This subclass of ``EmailMessage`` has an ``attach_alternative()`` method 
     307for including extra versions of the message body in the e-mail. All the other 
     308methods (including the class initialization) are inherited directly from 
     309``EmailMessage``. 
     310 
     311To 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 
     322By 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 
     324guarantees that any recipient will be able to read the e-mail, regardless of 
     325their mail client. However, if you are confident that your recipients can 
     326handle an alternative content type, you can use the ``content_subtype`` 
     327attribute on the ``EmailMessage`` class to change the main content type. The 
     328major type will always be ``"text"``, but you can change it to the subtype. For 
     329example:: 
     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 
     335SMTP network connections 
     336------------------------ 
    241337 
    242338The ``SMTPConnection`` class is initialized with the host, port, username and