Python/Django mime multipart email using boundary
Question: In Django(with python), how can I build an email that looks as follows:

What I need is an e-mail that starts with:

    
    
    Content-Type: multipart/alternative;
    boundary="=_5f8686714d769f9476ce778798b84ff4"
    

The boundary is the place to put plain text:

    
    
    --=_5f8686714d769f9476ce778798b84ff4
    Content-Type: text/plain; charset="ISO-8859-1"
    Content-Transfer-Encoding: 7bit
    
    This e-mail is in HTML-format. Click the following link: 
    
     http://www.company.nl/enews/7
    --=_5f8686714d769f9476ce778798b84ff4
    

After that I need an related Content-Tye:

    
    
    Content-Type: multipart/related;
    boundary="=_e49477d06c604958b9b2bc038628a26f"
    

The boundary is the place to put the html

    
    
    --=_e49477d06c604958b9b2bc038628a26f 
    Content-Type: text/html; charset="ISO-8859-1" 
    Content-Transfer-Encoding: quoted-printable
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.= w3.org/TR/html4/loose.dtd"> <html style=3D"height: 100%"> <head>/head> <body></body> </html>
    --=_e49477d06c604958b9b2bc038628a26f
    

Eventually it would look like this:

    
    
    Received: (qmail 6116 invoked by uid 48); 16 Jul 2012 18:00:49 +0200
    Date: 16 Jul 2012 18:00:49 +0200
    To: my@email.nl
    Subject: Something
    MIME-Version: 1.0
    From: Company <company@company.nl>
    
    Content-Type: multipart/alternative;
        boundary="=_5f8686714d769f9476ce778798b84ff4"
    Message-ID: <m79ghd.ezdd6g@company.nl>
    
    --=_5f8686714d769f9476ce778798b84ff4
    Content-Type: text/plain; charset="ISO-8859-1"
    Content-Transfer-Encoding: 7bit
    
    This e-mail is in HTML-format. Click the following link: 
    
     http://www.company.nl/enews/7
    --=_5f8686714d769f9476ce778798b84ff4
    
    Content-Type: multipart/related;
        boundary="=_e49477d06c604958b9b2bc038628a26f"
    
    --=_e49477d06c604958b9b2bc038628a26f
    Content-Type: text/html; charset="ISO-8859-1"
    Content-Transfer-Encoding: quoted-printable
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.=
    w3.org/TR/html4/loose.dtd">
    <html style=3D"height: 100%">
    <head>/head>
    <body></body>
    </html>
    --=_e49477d06c604958b9b2bc038628a26f
    

I tried this with Django's EmailMessage(Alternatives), python MimeMultipart
but I can't get it in the exact order.


Answer: If you are trying to send a plain text and HTML version of an e-mail (where
the plain text version just refers to an URL), the
[EmailMultiAlternatives](https://docs.djangoproject.com/en/1.4/topics/email/#sending-
alternative-content-types) class seems to work fine for me.

A slightly modified version of the example from the docs:

    
    
    from django.core.mail import EmailMultiAlternatives
    
    subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
    text_content = 'This e-mail is in HTML-format. Click the following link:\n\nhttp://www.company.nl/enews/7'
    html_content = '<p>This is an <strong>HTML-format</strong> version.</p>'
    msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
    msg.attach_alternative(html_content, "text/html")
    msg.send()
    

Results in this email (using the [console
backend](https://docs.djangoproject.com/en/1.4/topics/email/#console-
backend)):

    
    
    Content-Type: multipart/alternative;
     boundary="===============2514609180855632526=="
    MIME-Version: 1.0
    Subject: hello
    From: from@example.com
    To: to@example.com
    Date: Wed, 18 Jul 2012 10:31:22 -0000
    Message-ID: <20120718103122.11355.59803@localhost6.localdomain6>
    
    --===============2514609180855632526==
    Content-Type: text/plain; charset="utf-8"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    
    This e-mail is in HTML-format. Click the following link:
    
    http://www.company.nl/enews/7
    --===============2514609180855632526==
    Content-Type: text/html; charset="utf-8"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    
    <p>This is an <strong>HTML-format</strong> version.</p>
    --===============2514609180855632526==--
    

While it does not have the `multipart/related` Content-Type, it does work both
on plain text (showing the link) and HTML clients (showing the HTML version).

