简单的HTML电子邮件:剥离的基本CSS样式 [英] Simple HTML Email: Basic CSS styles being stripped
问题描述
我通过python脚本从linux机器上的命令行发送一个简单的电子邮件。我已经查找了为什么CSS可能会改变,剥离等在电子邮件客户端的答案。
I am sending a simple email from the command line on a linux machine through a python script. I have looked up answers about why CSS might get changed, stripped, etc. in email clients. However, I can't seem to solve what looks to me like a simple issue.
当我发送一个简单的HTML电子邮件与表,随机td的获取他们的样式剥离。当我在浏览器中渲染相同的HTML时,它看起来不错。
When I send a simple HTML email with a table, random td's get their styles stripped. When I render the same HTML in a browser, it looks fine.
这里是python代码:
Here is the python code:
#!/usr/bin/python
import os
import subprocess
def make_html_report(data, subject):
text = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
text += '<html lang="en"><head>'
text += '<meta charset="utf-8"/>'
text += '<title>{0}</title>'.format(subject)
text += '</head>'
text += '<body>'
text += '<table style="border:solid 1px #000000; border-collapse: collapse; width: 300px;">'
for row, line in enumerate(data):
tr_style = ""
if row == 0:
tr_style += "border-bottom:solid 1px #000000;"
text += '<tr style="{0}">'.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 += '<td style="{0}">{1}</td>'.format(td_style, item)
text += '</tr>'
text += '</table>'
text += '<p style="margin-top: 20px;">Random example email. Why is it not working??</p>'
text += '</body>'
text += '</html>'
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)随机空间被插入到样式元素,因此,它不呈现。示例:< td style =text-align:c enter:>
。在单词中心内插入空格。
I used an html checker. Everything was fine. But any time I send an email some or all of these things happen:
(1) td's lose their style element all together
(2) a random space is inserted into the style element and thus, it does not render. Example: <td style="text-align: c enter:>
. There is a space inserted within the word center.
有没有人知道这里发生了什么?
Does anyone have an idea about what is going on here?
推荐答案
根据RFC 2822 2.1.1 , http://www.faqs.org/rfcs/rfc2822.html ,每行的电子邮件应包含少于78个字符。
According to RFC 2822 2.1.1, http://www.faqs.org/rfcs/rfc2822.html, each line of an email SHOULD contain less than 78 characters.
尝试插入一些新行(\ n),以使任何行长度不超过78个字符
我认为这个函数可以做到(对不起,如果不是,我是一个PHP开发人员):
http://perldoc.perl.org/Text/Wrap.html
Try inserting some new lines (\n), so that no line is longer than 78 characters. I think this function can do that (Sorry, if not, I'm a PHP developer): http://perldoc.perl.org/Text/Wrap.html
这篇关于简单的HTML电子邮件:剥离的基本CSS样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!