在邮件中使用输入 - Python

Use input in mail - Python

我正在提示用户输入,但我不知道如何将其包含在 python 邮件中。这是消息字段:

message = """\ 
Subject: Info
{input} """

输入应该在我放的地方{input},我该如何实现?

您可以使用 f-string 格式化方法,在 Python 3.6 中引入。

message = f"""\ 
Subject: Info
{input()} """

从 python 3.6 开始,您可以使用 fstring(只需在 """ 之前放置一个 f),但出于安全原因,请不要忘记对输入的内容进行转义.这是 html 模块的示例,自 python 3.2 开始可用:

import html
# ...
message = f"""
Subject: Info
{html.escape(input)}
"""

否则旧的方法是使用.format():

import html
# ...
message = f"""
Subject: Info
{}
""".format(html.escape(input))

如果你是3.2以下的版本,那么你可以转义输入manually:

 text.replace('&', '&').replace('>', '>'
    ).replace('<','&lt;').replace('\'','&#39;'
    ).replace('"','&#34;').encode('ascii', 'xmlcharrefreplace') 

但除非您对 python 版本有严格限制,否则您应该使用最新版本的 python 3

您可以使用 python --version and/or python3 --version

检查您的 python 版本