【问题标题】:Simple HTML Email: Basic CSS styles being stripped简单的 HTML 电子邮件:基本的 CSS 样式被剥离
【发布时间】:2015-02-06 10:25:27
【问题描述】:

我正在通过 python 脚本从 linux 机器上的命令行发送一封简单的电子邮件。我已经查找了有关为什么 CSS 可能会在电子邮件客户端中更改、剥离等的答案。但是,我似乎无法解决在我看来很简单的问题。

当我发送带有表格的简单 HTML 电子邮件时,随机 td 的样式会被剥离。当我在浏览器中呈现相同的 HTML 时,它看起来很好。

这是python代码:

#!/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) 在样式元素中插入了一个随机空间,因此它不会呈现。示例:&lt;td style="text-align: c enter:&gt;。单词中心内插入了一个空格。

有人知道这里发生了什么吗?

【问题讨论】:

    标签: python html css email


    【解决方案1】:

    根据 RFC 2822 2.1.1 http://www.faqs.org/rfcs/rfc2822.html,电子邮件的每一行应包含少于 78 个字符。

    尝试插入一些新行 (\n),这样行的长度不会超过 78 个字符。 我认为这个函数可以做到这一点(对不起,如果不是,我是 PHP 开发人员): http://perldoc.perl.org/Text/Wrap.html

    【讨论】:

    • 哇,非常感谢。我花了很多时间试图解决这个问题,解决方案非常简单。在每个语句的末尾插入一个新行 \n 就可以了。非常感谢您的帮助。
    猜你喜欢
    • 2013-06-19
    • 2014-07-04
    • 2013-02-16
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多