Python Variable in an HTML email in Python

email, html-email, python, smtplib

Solution

Use `"formatstring".format`:

code = "We Says Thanks!"
html = """\
<html>
  <head></head>
  <body>
    <p>Thank you for being a loyal customer.<br>
       Here is your unique code to unlock exclusive content:<br>
       <br><br><h1>{code}</h1><br>
       <img src="http://example.com/footer.jpg">
    </p>
  </body>
</html>
""".format(code=code)

If you find yourself substituting a large number of variables, you can use

.format(**locals())

Problem

How do I insert a variable into an HTML email I'm sending with python? The variable I'm trying to send is `code`. Below is what I have so far. ``` text = "We Says Thanks!" html = """\ <html> <head></head> <body> <p>Thank you for being a loyal customer.<br> Here is your unique code to unlock exclusive content:<br> <br><br><h1><% print code %></h1><br> <img src="http://example.com/footer.jpg"> </p> </body> </html> """ ```

Original source