Reputation: 2370
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: [email protected]
Subject: Something
MIME-Version: 1.0
From: Company <[email protected]>
Content-Type: multipart/alternative;
boundary="=_5f8686714d769f9476ce778798b84ff4"
Message-ID: <[email protected]>
--=_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.
Upvotes: 2
Views: 2588
Reputation: 13001
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 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', '[email protected]', '[email protected]'
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):
Content-Type: multipart/alternative;
boundary="===============2514609180855632526=="
MIME-Version: 1.0
Subject: hello
From: [email protected]
To: [email protected]
Date: Wed, 18 Jul 2012 10:31:22 -0000
Message-ID: <[email protected]>
--===============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).
Upvotes: 1
Reputation: 77349
Use the email and smtplib modules directly.
Django built-in methods for email handling are very limited.
Upvotes: 0