Home > other >  Use input in mail - Python
Use input in mail - Python

Time:01-28

I am prompting a user for input, but I have no idea how to include it in a python mail. This is the message field:

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

The input is supposed to be where I put {input}, how do I implement it?

CodePudding user response:

You can use an fstring since python 3.6 (just put a f before the """), but don't forget to escape the content of the input for security reasons. Here is an example with the html module, available since python 3.2 :

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

Otherwise the old way is to use .format():

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

if you are below version 3.2, then you can escape the input manually:

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

But except if you have heavy restrictions on python version, you should be using a recent version of python 3.

you can check your python version with python --version and/or python3 --version

CodePudding user response:

You can use the f-string formatting method, introduced with Python 3.6.

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