如何将变量传递给邮件脚本中的html?

时间:2013-05-29 09:46:51

标签: python

我的代码是这样的:

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

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

如何在html中将“用户名”传递给“名字”? 如何在html中编写代码?

1 个答案:

答案 0 :(得分:1)

使用string templates

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

HTML示例:

>>> 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>'