Python variable in HTML email in Python

How to insert a variable into the html email address that I am sending using python? The variable I'm trying to send is code . Below I still know.

 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> """ 
+4
source share
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()) 
+16
source

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 and not substitute , as if there was no placeholder in this dictionary, substitute raises a ValueError: Invalid placeholder in string . The same problem is related to string formatting .

+7
source

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

usually the% operator is used to put a variable in a string,% i for integers,% s for strings and% f for floats, NB: there is also another type of formatting (.format), which is also described in the link above, which allows you to pass to a dict or list, a little more elegant than what I will show below, it may be something that you should use in a long run, since the% operator gets confused if you have 100 variables that you want to put in a string, although using dicts (my last example) like this negates u about.

 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> 

it is very simple and only works with primitive types, if you want to be able to store dicts, lists and possible objects, I suggest you use cobvert them for 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

+1
source

Source: https://habr.com/ru/post/1443806/


All Articles