Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Variable in an HTML email in Python

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>
"""
like image 537
David Vasandani Avatar asked Nov 03 '12 10:11

David Vasandani


3 Answers

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://domain.com/footer.jpg">
    </p>
  </body>
</html>
""".format(code=code)

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

.format(**locals())
like image 194
Eric Avatar answered Sep 30 '22 16:09

Eric


Another way is to use Templates:

>>> from string import Template
>>> 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>
'''
>>> s = Template(html).safe_substitute(code="We Says Thanks!")
>>> print(s)
<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>We Says Thanks!</h1><br>
       <img src="http://example.com/footer.jpg">
    </p>
  </body>
</html>

Note, that I used safe_substitute, not substitute, as if there is a placeholder which is not in the dictionary provided, substitute will raise ValueError: Invalid placeholder in string. The same problem is with string formatting.

like image 20
ovgolovin Avatar answered Sep 30 '22 15:09

ovgolovin


use pythons string manipulation: http://docs.python.org/2/library/stdtypes.html#string-formatting

generally the % operator is used to put a variable into a string, %i for integers, %s for strings and %f for floats, NB: there is also another formatting type (.format) which is also described in the above link, that allows you to pass in a dict or list slightly more elegant than what I show below, this may be what you should go for in the long run as the % operator gets confusing if you have 100 variables you want to put into a string, though the use of dicts (my last example) kinda negates this.

code_str = "super duper heading"
html = "<h1>%s</h1>" % code_str
# <h1>super duper heading</h1>
code_nr = 42
html = "<h1>%i</h1>" % code_nr
# <h1>42</h1>

html = "<h1>%s %i</h1>" % (code_str, code_nr)
# <h1>super duper heading 42</h1>

html = "%(my_str)s %(my_nr)d" %  {"my_str": code_str, "my_nr": code_nr}
# <h1>super duper heading 42</h1>

this is very basic and only work with primitive types, if you want to be able to store dicts, lists and possible objects I suggest you use cobvert them to jsons http://docs.python.org/2/library/json.html and https://stackoverflow.com/questions/4759634/python-json-tutorial are good sources of inspiration

Hope this helps

like image 33
jcr Avatar answered Sep 30 '22 16:09

jcr