Django

Code

Changeset 5548

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

Added support for creating multipart/alternative email messages. Also allow
tweaking of main body MIME subtype for brave people. Fixed #3605.

Files:

Legend:

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

    r5547 r5548  
    170170    A container for email information. 
    171171    """ 
     172    content_subtype = 'plain' 
     173    multipart_subtype = 'mixed' 
     174 
    172175    def __init__(self, subject='', body='', from_email=None, to=None, bcc=None, 
    173176            connection=None, attachments=None): 
     
    186189 
    187190    def message(self): 
    188         msg = SafeMIMEText(self.body, 'plain', settings.DEFAULT_CHARSET) 
     191        msg = SafeMIMEText(self.body, self.content_subtype, settings.DEFAULT_CHARSET) 
    189192        if self.attachments: 
    190193            body_msg = msg 
    191             msg = SafeMIMEMultipart(
     194            msg = SafeMIMEMultipart(_subtype=self.multipart_subtype
    192195            if self.body: 
    193196                msg.attach(body_msg) 
     
    217220        return self.get_connection(fail_silently).send_messages([self]) 
    218221 
    219     def attach(self, filename, content=None, mimetype=None): 
    220         """ 
    221         Attaches a file with the given filename and content. 
    222  
    223         Alternatively, the first parameter can be a MIMEBase subclass, which 
    224         is inserted directly into the resulting message attachments. 
     222    def attach(self, filename=None, content=None, mimetype=None): 
     223        """ 
     224        Attaches a file with the given filename and content. The filename can 
     225        be omitted (useful for multipart/alternative messages) and the mimetype 
     226        is guessed, if not provided. 
     227 
     228        If the first parameter is a MIMEBase subclass it is inserted directly 
     229        into the resulting message attachments. 
    225230        """ 
    226231        if isinstance(filename, MIMEBase): 
     232            assert content == mimetype == None 
    227233            self.attachements.append(filename) 
    228234        else: 
     
    253259            attachment.set_payload(content) 
    254260            Encoders.encode_base64(attachment) 
    255         attachment.add_header('Content-Disposition', 'attachment', filename=filename) 
     261        if filename: 
     262            attachment.add_header('Content-Disposition', 'attachment', filename=filename) 
    256263        return attachment 
     264 
     265class EmailMultiAlternatives(EmailMessage): 
     266    """ 
     267    A version of EmailMessage that makes it easy to send multipart/alternative 
     268    messages. For example, including text and HTML versions of the text is 
     269    made easier. 
     270    """ 
     271    multipart_subtype = 'alternative' 
     272 
     273    def attach_alternative(self, content, mimetype=None): 
     274        """Attach an alternative content representation.""" 
     275        self.attach(content=content, mimetype=mimetype) 
    257276 
    258277def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None): 
  • django/trunk/docs/email.txt

    r5547 r5548  
    282282        message.attach_file('/images/weather_map.png') 
    283283 
     284Sending alternative content types 
     285~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     286 
     287It is often useful to include multiple versions of the content in an e-mail. 
     288For instance, sending both text and HTML versions of an e-mail. You can do 
     289this using the ``EmailMultiAlternatives`` class. This sub-class of 
     290``EmailMessage`` has an ``attach_alternative()`` method for including 
     291extra versions of the message body in the e-mail. All the other methods 
     292(including the class initialization) are inherited directly from 
     293``EmailMessage``. 
     294 
     295To send a text and HTML combination, you could write:: 
     296 
     297    from django.core.mail import EmailMultiAlternatives 
     298 
     299    subject, from_email, to = ... 
     300    text_content = "This is an important message." 
     301    html_content = "<p>This is an <strong>important</strong> message." 
     302    msg = EmailMultiAlternatives(subject, text_content, from_email, to) 
     303    msg.attach_alternative(html_content, "text/html") 
     304    msg.send() 
     305 
     306 
     307 
     308By default, the MIME type of the ``body`` parameter in an ``EmailMessage`` is 
     309``"text/plain"``. It is good practice to leave this alone, since it guarantees 
     310that any recipient will be able to read the e-mail, regardless of their mail 
     311client. However, if you are confident that your recipients can handle an 
     312alternative content type, you can use the ``content_subtype`` attribute on the 
     313``EmailMessage`` class to change the main content type. The major type will 
     314always be ``"text"``, but you can change to the subtype. For example:: 
     315 
     316    msg = EmailMessage(subject, html_content, from_email, to) 
     317    msg.content_subtype = "html"  # Main content is now text/html 
     318    msg.send() 
     319 
    284320SMTP network connections 
    285321-------------------------