Python Variable in an HTML email in Python

情到浓时终转凉″ 提交于 2019-12-04 19:32:44

问题


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://domain.com/footer.jpg">
    </p>
  </body>
</html>
"""

回答1:


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())



回答2:


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://domain.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://domain.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.




回答3:


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



来源:https://stackoverflow.com/questions/13208212/python-variable-in-an-html-email-in-python

工具导航Map