html in django password_reset_email.html

523 views
Skip to first unread message

andy

unread,
Nov 2, 2010, 5:07:54 AM11/2/10
to Django users
I recently tried to send the following in a test password email from a
site I am creating:

<h1>This is a test reset password email.</h1>

However when I received the email it displayed the "<h1>This is a test
reset password email.</h1>".

My question is how can I send a styled email using html tags and
possibly even css when using the password_reset_email.html template
that goes with django's view.password_reset?

Tom Evans

unread,
Nov 2, 2010, 5:50:49 AM11/2/10
to django...@googlegroups.com

I don't know about the specifics of your particular issues, but to
send HTML emails:

Create an instance of django.core.mail.EmailMultiAlternatives, passing
in your subject, text version of the email, from address, list of
recipients. Render your HTML version of the email, and attach it to
the email, and then send it.

eg:

from django.core.mail import EmailMultiAlternatives
msg = EmailMultiAlternatives('Subject', 'This is the text version',
'fr...@address.com', [ 'recip...@address.com',])
html_version = ...
msg.attach_alternative(html_version, 'text/html')
msg.send()

There are lots of 'gotchas' when sending HTML email. Don't expect
externally referenced resources to be available, don't expect styles
to work correctly in all UAs, don't expect any JS to work.

Cheers

Tom

andy

unread,
Nov 2, 2010, 6:11:33 PM11/2/10
to Django users


On Nov 2, 4:50 am, Tom Evans <tevans...@googlemail.com> wrote:
> On Tue, Nov 2, 2010 at 9:07 AM, andy <flowar...@gmail.com> wrote:
> > I recently tried to send the following in a test password email from a
> > site I am creating:
>
> > <h1>This is a test reset password email.</h1>
>
> > However when I received the email it displayed the "<h1>This is a test
> > reset password email.</h1>".
>
> > My question is how can I send a styled email using html tags and
> > possibly even css when using the password_reset_email.html template
> > that goes with django's view.password_reset?
>
> I don't know about the specifics of your particular issues, but to
> send HTML emails:
>
> Create an instance of django.core.mail.EmailMultiAlternatives, passing
> in your subject, text version of the email, from address, list of
> recipients. Render your HTML version of the email, and attach it to
> the email, and then send it.
>
> eg:
>
> from django.core.mail import EmailMultiAlternatives
> msg = EmailMultiAlternatives('Subject', 'This is the text version',
> 'f...@address.com', [ 'recipie...@address.com',])
> html_version = ...
> msg.attach_alternative(html_version, 'text/html')
> msg.send()
>
> There are lots of 'gotchas' when sending HTML email. Don't expect
> externally referenced resources to be available, don't expect styles
> to work correctly in all UAs, don't expect any JS to work.
>
> Cheers
>
> Tom

Thanks for the reply, Django password_reset view used send_mail()
which seem to send only text. apparently you have to use
EmailMultiAlternatives as you pointed out to send html content guess I
will have to create my own view and use this instead if I want to send
html. Then again it is probably a good idea to send password reset
information in plant text. Since it pretty any email client would be
able to read plain text.

Thanks again

Kimani

andy

unread,
Nov 2, 2010, 6:11:49 PM11/2/10
to Django users


On Nov 2, 4:50 am, Tom Evans <tevans...@googlemail.com> wrote:
> On Tue, Nov 2, 2010 at 9:07 AM, andy <flowar...@gmail.com> wrote:
> > I recently tried to send the following in a test password email from a
> > site I am creating:
>
> > <h1>This is a test reset password email.</h1>
>
> > However when I received the email it displayed the "<h1>This is a test
> > reset password email.</h1>".
>
> > My question is how can I send a styled email using html tags and
> > possibly even css when using the password_reset_email.html template
> > that goes with django's view.password_reset?
>
> I don't know about the specifics of your particular issues, but to
> send HTML emails:
>
> Create an instance of django.core.mail.EmailMultiAlternatives, passing
> in your subject, text version of the email, from address, list of
> recipients. Render your HTML version of the email, and attach it to
> the email, and then send it.
>
> eg:
>
> from django.core.mail import EmailMultiAlternatives
> msg = EmailMultiAlternatives('Subject', 'This is the text version',
> 'f...@address.com', [ 'recipie...@address.com',])
> html_version = ...
> msg.attach_alternative(html_version, 'text/html')
> msg.send()
>
> There are lots of 'gotchas' when sending HTML email. Don't expect
> externally referenced resources to be available, don't expect styles
> to work correctly in all UAs, don't expect any JS to work.
>
> Cheers
>
> Tom

andy

unread,
Nov 2, 2010, 6:12:28 PM11/2/10
to Django users


On Nov 2, 4:50 am, Tom Evans <tevans...@googlemail.com> wrote:
> On Tue, Nov 2, 2010 at 9:07 AM, andy <flowar...@gmail.com> wrote:
> > I recently tried to send the following in a test password email from a
> > site I am creating:
>
> > <h1>This is a test reset password email.</h1>
>
> > However when I received the email it displayed the "<h1>This is a test
> > reset password email.</h1>".
>
> > My question is how can I send a styled email using html tags and
> > possibly even css when using the password_reset_email.html template
> > that goes with django's view.password_reset?
>
> I don't know about the specifics of your particular issues, but to
> send HTML emails:
>
> Create an instance of django.core.mail.EmailMultiAlternatives, passing
> in your subject, text version of the email, from address, list of
> recipients. Render your HTML version of the email, and attach it to
> the email, and then send it.
>
> eg:
>
> from django.core.mail import EmailMultiAlternatives
> msg = EmailMultiAlternatives('Subject', 'This is the text version',
> 'f...@address.com', [ 'recipie...@address.com',])
> html_version = ...
> msg.attach_alternative(html_version, 'text/html')
> msg.send()
>
> There are lots of 'gotchas' when sending HTML email. Don't expect
> externally referenced resources to be available, don't expect styles
> to work correctly in all UAs, don't expect any JS to work.
>
> Cheers
>
> Tom

andy

unread,
Nov 2, 2010, 6:17:52 PM11/2/10
to Django users


On Nov 2, 4:50 am, Tom Evans <tevans...@googlemail.com> wrote:
> On Tue, Nov 2, 2010 at 9:07 AM, andy <flowar...@gmail.com> wrote:
> > I recently tried to send the following in a test password email from a
> > site I am creating:
>
> > <h1>This is a test reset password email.</h1>
>
> > However when I received the email it displayed the "<h1>This is a test
> > reset password email.</h1>".
>
> > My question is how can I send a styled email using html tags and
> > possibly even css when using the password_reset_email.html template
> > that goes with django's view.password_reset?
>
> I don't know about the specifics of your particular issues, but to
> send HTML emails:
>
> Create an instance of django.core.mail.EmailMultiAlternatives, passing
> in your subject, text version of the email, from address, list of
> recipients. Render your HTML version of the email, and attach it to
> the email, and then send it.
>
> eg:
>
> from django.core.mail import EmailMultiAlternatives
> msg = EmailMultiAlternatives('Subject', 'This is the text version',
> 'f...@address.com', [ 'recipie...@address.com',])
> html_version = ...
> msg.attach_alternative(html_version, 'text/html')
> msg.send()
>
> There are lots of 'gotchas' when sending HTML email. Don't expect
> externally referenced resources to be available, don't expect styles
> to work correctly in all UAs, don't expect any JS to work.
>
> Cheers
>
> Tom

larry....@gmail.com

unread,
Nov 21, 2012, 10:27:05 AM11/21/12
to django...@googlegroups.com, teva...@googlemail.com
On Tuesday, November 2, 2010 5:50:49 AM UTC-4, Tom Evans wrote:
On Tue, Nov 2, 2010 at 9:07 AM, andy <flow...@gmail.com> wrote:
> I recently tried to send the following in a test password email from a
> site I am creating:
>
> <h1>This is a test reset password email.</h1>
>
> However when I received the email it displayed the "<h1>This is a test
> reset password email.</h1>".
>
> My question is how can I send a styled email using html tags and
> possibly even css when using the password_reset_email.html template
> that goes with django's view.password_reset?
>

I don't know about the specifics of your particular issues, but to
send HTML emails:

Create an instance of django.core.mail.EmailMultiAlternatives, passing
in your subject, text version of the email, from address, list of
recipients. Render your HTML version of the email, and attach it to
the email, and then send it.

eg:

from django.core.mail import EmailMultiAlternatives
msg = EmailMultiAlternatives('Subject', 'This is the text version',
'fr...@address.com', [ 'recip...@address.com',])
html_version = ...
msg.attach_alternative(html_version, 'text/html')
msg.send()


I am having the same issue, but it's not clear to me how to implement your solution. I have added the files password_reset_email.html, activation_email_subject.txt, password_reset_form.html, and password_reset_done.html to my ui/templates/registration dir customized them as needed. Where would I put your code above? Where would I call it from?

Thanks!
-larry 

Tom Evans

unread,
Nov 21, 2012, 11:04:43 AM11/21/12
to larry....@gmail.com, django...@googlegroups.com
When you use the stock d.c.a.password_reset view, one of the arguments
you can provide is a custom Form class to handle the password reset.
You would provide this as an additional argument supplied in the
urlconf.

The form is what actually sends the email, in the save() method, so by
sub-classing the default form, and overriding the save() method, you
can handle how the email is produced. You would need to duplicate most
of the logic from the default form's save() method, unfortunately.

Cheers

Tom

Larry Martell

unread,
Nov 21, 2012, 3:23:08 PM11/21/12
to Tom Evans, django...@googlegroups.com
Thanks for the reply, Tom. I'll have to dig into this next week when I
am recovered from turkey stupor (you don't do silly thinks like that
in the UK ;-)
Reply all
Reply to author
Forward
0 new messages