我通过一个python脚本从linux机器上的命令行发送一封简单的电子邮件。我已经在电子邮件客户端中查找了CSS为什么会被更改、删除等问题的答案。然而,我似乎无法解决我看来很简单的问题。在
当我发送一封带有表格的简单HTML电子邮件时,随机td的风格就会被剥离。当我在浏览器中呈现相同的HTML时,它看起来很好。在
下面是python代码:#!/usr/bin/python
import os
import subprocess
def make_html_report(data, subject):
text = ''
text += '
'text += ''
text += '
{0}'.format(subject)text += ''
text += '
'text += '
for row, line in enumerate(data):
tr_style = ""
if row == 0:
tr_style += "border-bottom:solid 1px #000000;"
text += '
'.format(tr_style)for index, item in enumerate(line):
td_style = "border-right:solid 1px #000000;"
if row != 0:
if index == 1:
td_style += "text-align:right; padding-right:5px;"
if float(line[3]) < 0:
td_style += "color:#ff0000;"
if index != 1 or row == 0:
td_style += "text-align:center;"
text += '
{1}'.format(td_style, item)text += '
'text += '
'text += '
Random example email. Why is it not working??
'text += ''
text += ''
print text
return text
def send_report(html_content, subject):
SENDMAIL = "/usr/sbin/sendmail" # sendmail location
p = os.popen("%s -t" % SENDMAIL, "w")
text = "From: {0}\nTo: {1}\nSubject: {2}\nContent-Type: text/html\nMIME-Version: 1.0\n\n{3}".format(
"me@example.com",
"me@example.com",
subject,
html_content
)
print "\n{0}".format(text)
p.write(text)
sts = p.close()
if __name__ == "__main__":
subject = "example subject"
data = [["head1","head2","head3","head4"], [1,2,3,4], [4,3,2,1], [8,7,6,4], [6,5,4,-5], [5,4,3,-2], [8,7,6,4], [6,5,4,-5], [5,4,3,-2]]
send_report(make_html_report(data, subject), subject)
我用了一个html检查器。一切都很好。但每当我发邮件时,都会发生一些或所有这些事情:
(1) td完全失去了他们的风格元素
(2) 在样式元素中插入一个随机空格,因此它不会呈现。示例: