我正在尝试使用 python、MIMEMultipart 和 smtp 发送 html 电子邮件。 html 很长,所以我将所有 html 放在一个 html 文件中。现在我想将 html 文件导入我的 python 文件(在同一文件夹中)并将所有 html 设置为一个名为 html_string 的 python 变量中的字符串。
html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome to infinity2o</title>
<style type="text/css">
body {
padding-top: 0 !important;
padding-bottom: 0 !important;
padding-top: 0 !important;
padding-bottom: 0 !important;
margin: 0 !important;
width: 100% !important;
-webkit-text-size-adjust: 100% !important;
-ms-text-size-adjust: 100% !important;
-webkit-font-smoothing: antialiased !important;
}
.
.
.
</head>
</html>
如何将所有 html 导入我的 python 文件并将其设置为等于一个变量:
python :
html_string = """
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Welcome to infinity2o</title>
<style type="text/css">
body {
padding-top: 0 !important;
padding-bottom: 0 !important;
padding-top: 0 !important;
padding-bottom: 0 !important;
margin: 0 !important;
width: 100% !important;
-webkit-text-size-adjust: 100% !important;
-ms-text-size-adjust: 100% !important;
-webkit-font-smoothing: antialiased !important;
}
.
.
.
</head>
</html> """
最佳答案
您可以打开文件,读取其所有内容,并将其设置为一个变量:
with open('html_file.html', 'r') as f:
html_string = f.read()
关于python - 如何将html文件导入python变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51794609/