Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-使用变量发送HTML邮件_Python_Html_Email_String Formatting - Fatal编程技术网

Python-使用变量发送HTML邮件

Python-使用变量发送HTML邮件,python,html,email,string-formatting,Python,Html,Email,String Formatting,我想做一个小的cronmailer供我个人使用。这是不起作用的部分 # Create the body of the message (a plain-text and an HTML version). text = "LOG OUTPUT: " + sys.argv[1] + "\n" logdata = open(sys.argv[2], "rb") for row in logdata: text = text + row html = """\ <html&g

我想做一个小的cronmailer供我个人使用。这是不起作用的部分

# Create the body of the message (a plain-text and an HTML version).
text = "LOG OUTPUT: " + sys.argv[1] + "\n"
logdata = open(sys.argv[2], "rb") 
for row in logdata:
        text = text + row

html = """\
<html>
  <head></head>
  <body>
    <p>LOG OUTPUT: {0} <br> {1} 
    </p>
  </body>
</html>
""".format(unicode(str(sys.argv[1]),'utf-8'),  unicode(str(logdata),'utf-8'))


part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

msg.attach(part1)
msg.attach(part2)

server = smtplib.SMTP(str(server) + ":" + str(port))
server.starttls()
server.login(username,password)
server.sendmail(emailfrom, emailto, msg.as_string())
server.quit()   
#创建邮件正文(纯文本和HTML版本)。
text=“日志输出:”+sys.argv[1]+“\n”
日志数据=打开(sys.argv[2],“rb”)
对于日志数据中的行:
文本=文本+行
html=”“”\
日志输出:{0}
{1}

.format(unicode(str(sys.argv[1]),'utf-8'),unicode(str(logdata),'utf-8')) part1=MIMEText(文本“纯”) part2=MIMEText(html,'html') 附加信息(第1部分) 附加信息(第2部分) server=smtplib.SMTP(str(服务器)+“:”+str(端口)) server.starttls() server.login(用户名、密码) server.sendmail(emailfrom、emailto、msg.as_string()) server.quit()
我收到了邮件。使用纯文本和附件(省略代码,因为它可以工作)。但不是HTML部分。我在那里得到的唯一东西是:

<html>
  <head></head>
  <body>
    <p>LOG OUTPUT: test

日志输出:测试

感谢您的帮助并祝您新年快乐

以下是构建html字符串的更好方法(在Python 2中):

导入cgi
text=“日志输出:”+sys.argv[1]+“\n”
打开(sys.argv[2],“rb”)作为f:
text+=f.read()
html=”“”\
日志输出:{0}
{1}

.format(unicode(str(sys.argv[1]),'utf-8'), unicode(cgi.escape(文本),'utf-8'))

请注意,在Python3中,转义函数是
html.escape
(因此最好不要命名自己的变量
html
,因为它会发生冲突:-),但在Python2中,转义函数是专门命名的
cgi.escape
,这应该没问题。

我忍不住注意到输出正好在您没有关闭的

元素开始的地方结束。如果幸运的话,将其更改为

实际上可能会解决您的问题。是否要包含文件的内容?afaik,unicode(str(logdata),'utf-8')不返回file@FrEaKmAn如何将文件的内容直接添加到HTML中?它只包含文本。使用logdata.Read()读取文件。@SvenBamberger,您在变量
text
中有“文件的内容”(按照FrEaKmAn的建议,在
.Read()
时非常费力地获得),这会更简单,但这是一个细节)——因此,请转义并插入它。逃跑很重要,但你现在还没做!您正在插入一个字符串,例如
,而这些未被替换的尖括号无疑会导致您的问题…只需将此日志输出:{0}
{1}修改为{0},以防止双线日志输出:XXXX
import cgi

text = "LOG OUTPUT: " + sys.argv[1] + "\n"
with open(sys.argv[2], "rb") as f:
    text += f.read()

html = """\
<html>
  <head></head>
  <body>
    <p>LOG OUTPUT: {0}<br/>
    <pre>{1}</pre>
    </p>
  </body>
</html>""".format(unicode(str(sys.argv[1]), 'utf-8'),
                  unicode(cgi.escape(text), 'utf-8'))