tags:

views:

41

answers:

1

Hi all,

Can anyone please help me sending html email with dynamic contents. One way is to copy the entire html code into a variable and populate the dynamic code within it in Django views, but that does not seem to be a good idea, as its a very large html file.

I would appreciate any suggestions.

Thanks.

+2  A: 

This should do what you want:

from django.core.mail import EmailMessage
from django.template import Context
from django.template.loader import get_template


template = get_template('myapp/email.html')
context = Context({'user': user, 'other_info': info})
content = template.render(context)
if not user.email:
    raise BadHeaderError('No email address given for {0}'.format(user))
msg = EmailMessage(subject, content, from, to=[user.email,])
msg.send()

See the django mail docs for more.

blokeley
Or as I said, for the first 3 lines, use the render_to_string shortcut, it exists for a reason.
KillianDS
Thanks, I shall use that in my project, and (if I remember) edit my answer once I've tested it.
blokeley