Ying.Zhao
Ying.Zhao

Reputation: 164

How to pass a variable to html in a mail script?

My code like this:

success_msg='''\
<html>
    Hi. {{ name }}
    ...
'''
def sendmail(sender,receiver,title,content,username):
    ...

if __name__ == '__main__':
    sendmail(XXX,XXX,XXX,success_msg,usernameXX)

How to pass the "username" to "name" in html? How to write the code in html?

Upvotes: 0

Views: 1109

Answers (1)

Burhan Khalid
Burhan Khalid

Reputation: 174622

Use string templates:

>>> from string import Template
>>> msg = Template("Hi $name")
>>> msg.substitute(name='World')
'Hi World'

HTML Example:

>>> header = '<html><head><title>Sample</title></head><body>'
>>> body = '<p>Hello <strong>$name</strong></p>'
>>> footer = '</body></html>'
>>> html = Template(header+body+footer)
>>> html.substitute(name='World')
'<html><head><title>Sample</title></head><body><p>Hello <strong>World</strong></
p></body></html>'

Upvotes: 1

Related Questions