In Django, Sending emails

Source Code: Link

Extending our previous article “Build a simple Django contact form that sends emails” we will now add a template that can handle dynamic data.

To add a template we need to add two simple components:

  1. render_to_string() function from django.template.loader
  2. A HMTL template

Lets start with our template

First lets create a file called email.html and add some varibles. We will create these in the next step.

No in our view lets import render_to_string,  create some context and pass it into our template.

Go ahead and replace the message variable with our template variable

No when we send a message our email should look like this:

Contact Us

We're not around right now. But you can send us an email and we'll get back to you, asap.