Django

Code

Changeset 5550

Show
Ignore:
Timestamp:
06/27/07 07:41:37 (1 year ago)
Author:
mtredinnick
Message:

Fixed #3985 -- Added support for custom email headers.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/mail.py

    r5548 r5550  
    174174 
    175175    def __init__(self, subject='', body='', from_email=None, to=None, bcc=None, 
    176             connection=None, attachments=None): 
     176            connection=None, attachments=None, headers=None): 
    177177        self.to = to or [] 
    178178        self.bcc = bcc or [] 
     
    181181        self.body = body 
    182182        self.attachments = attachments or [] 
     183        self.extra_headers = headers or {} 
    183184        self.connection = connection 
    184185 
     
    207208        if self.bcc: 
    208209            msg['Bcc'] = ', '.join(self.bcc) 
     210        for name, value in self.extra_headers.items(): 
     211            msg[name] = value 
    209212        return msg 
    210213 
  • django/trunk/docs/email.txt

    r5549 r5550  
    219219---------------- 
    220220 
    221 The ``EmailMessage`` class is initialized as follows:: 
    222  
    223     email = EmailMessage(subject, body, from_email, to, 
    224                          bcc, connection, attachments) 
    225  
    226 All of these parameters are optional. If ``from_email`` is omitted, the value 
    227 from ``settings.DEFAULT_FROM_EMAIL`` is used. Both the ``to`` and ``bcc`` 
    228 parameters are lists of addresses, as strings. The ``attachments`` parameter is 
    229 a list containing either ``(filename, content, mimetype)`` triples of 
    230 ``email.MIMEBase.MIMEBase`` instances. 
     221The ``EmailMessage`` class is initialized with the following parameters (in 
     222the given order, if positional arguments are used). All parameters are 
     223optional and can be set at any time prior to calling the ``send()`` method. 
     224 
     225    * ``subject``: The subject line of the e-mail. 
     226 
     227    * ``body``: The body text. This should be a plain text message. 
     228 
     229    * ``from_email``: The sender's address. Both ``fred@example.com`` and 
     230      ``Fred <fred@example.com>`` forms are legal. If omitted, the 
     231      ``DEFAULT_FROM_EMAIL`` setting is used. 
     232 
     233    * ``to``: A list or tuple of recipient addresses. 
     234 
     235    * ``bcc``: A list or tuple of addresses used in the "Bcc" header when 
     236      sending the e-mail. 
     237 
     238    * ``connection``: An ``SMTPConnection`` instance. Use this parameter if 
     239      you want to use the same conneciton for multiple messages. If omitted, a 
     240      new connection is created when ``send()`` is called. 
     241 
     242    * ``attachments``: A list of attachments to put on the message. These can 
     243      be either ``email.MIMEBase.MIMEBase`` instances, or ``(filename, 
     244      content, mimetype)`` triples. 
     245 
     246    * ``headers``: A dictionary of extra headers to put on the message. The 
     247      keys are the header name, values are the header values. It is up to the 
     248      caller to ensure header names and values are in the correct format for 
     249      an e-mail message. 
    231250 
    232251For example:: 
    233252 
    234253    email = EmailMessage('Hello', 'Body goes here', 'from@example.com', 
    235                 ['to1@example.com', 'to2@example.com'], 
    236                 ['bcc@example.com']
     254                ['to1@example.com', 'to2@example.com'], ['bcc@example.com'], 
     255                headers = {'Reply-To: 'another@example.com'}
    237256 
    238257The class has the following methods: