Sending html email address in Django

Since Django 1.7 came out, things have changed a bit. I am trying to use send_mail to send HTML emails.

I want to send a thank you note to users after registering on my site.

Im using

subject = 'Thank you from ******'
message = 'text version of HTML message'
from_email = my email address
to_list = users email address
html_message= really long set of html code

send_mail(subject,message,from_email,to_list,fail_silently=True,html_message=html_message) 

Is it possible to store html as a file on the server and then convert it to a string so that it can be served in "html_message"?

+4
source share
1 answer

Yes, you can. In my own project, I use the following code to do the same:

from django.template import loader

html_message = loader.render_to_string(
            'path/to/your/htm_file.html',
            {
                'user_name': user.name,
                'subject':  'Thank you from' + dynymic_data,
                //...  
            }
        )
send_mail(subject,message,from_email,to_list,fail_silently=True,html_message=html_message)

And the html file looks like this:

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <h1>{{ user_name }}</h1>
        <h2>{{ subject }}</h2>
    </body>
</html>
+7
source

All Articles