Sending text and HTML messages

5 views
Skip to first unread message

Fabien Schwob

unread,
Aug 16, 2007, 10:42:47 AM8/16/07
to django...@googlegroups.com
Hello,

Since I'm about to use django-mailer in a project, I've looked to the
source code in order to try to use it. I currently only have only
plain text messages, but I will use HTML messages very soon and I
would like to know the approach that had been taken.

It seems that with the current design, I will create a Message
instance with nearly the entire mail source as the message body (the
text version, the html version and the additional headers).

Am I wrong ?

PS: Is it safe to start to use django-mailer today ?

Thanks in advance

--
Fabien SCHWOB

James Tauber

unread,
Aug 16, 2007, 11:23:31 AM8/16/07
to django...@googlegroups.com

There's still a bit of work to get django-mailer usable. For a start, it
doesn't actually send mail, although that's a fairly easy fix (import
django.core.mail.send_mail and use that where the current print is). Are
you needing to templatize your email or send exactly the same thing to
each person?

I haven't done anything with HTML messages yet, although that's
obviously something we'll need to support.

James

On Thu, 16 Aug 2007 16:42:47 +0200, "Fabien Schwob" <xphu...@gmail.com>
said:

--
James Tauber http://jtauber.com/
journeyman of some http://jtauber.com/blog/

Fabien Schwob

unread,
Aug 17, 2007, 4:54:23 AM8/17/07
to django...@googlegroups.com
Hello,

> I haven't done anything with HTML messages yet, although that's
> obviously something we'll need to support.

I've founded how to support HTML in django-mailer. The solution is to
build the message with the Django provided classes
(EmailMultiAlternatives and EmailMessage).

>>> # Code from the Django docs
>>> from django.core.mail import EmailMultiAlternatives
>>>
>>> subject, from_email, to = 'hello', 'fr...@example.com', 't...@example.com'
>>> text_content = 'This is an important message.'
>>> html_content = '<p>This is an <strong>important</strong> message.</p>'
>>> msg = EmailMultiAlternatives(subject, text_content, from_email, to)
>>> msg.attach_alternative(html_content, "text/html")

At this point, you have an EmailMultiAlternatives instance. You can
get a django.core.mail.SafeMIMEText object (a subclass of Python's
email.MIMEText.MIMEText class) or a django.core.mail.SafeMIMEMultipart
by calling message(). And since the Python message object has a
as_string() methods, you can get the message that you would store in
django-mailer

And this point, you loop through the message and send them with
smtplib (which take the full message as parameter).

Is it a good solution ?

--
Fabien SCHWOB

Reply all
Reply to author
Forward
0 new messages