Simple HTML Email: Basic CSS Styles

I am sending simple email from the command line on a linux machine through a python script. I was looking for answers about why CSS can be changed, stripped, etc. In mail clients. However, I cannot decide what seems to me like a simple problem.

When I send a simple HTML email with a table, random td erase their styles. When I create the same HTML in the browser, it looks fine.

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 checker. . , , : (1) td (2) , , . : <td style="text-align: c enter:>. .

- , ?

0
1
+1

Source: https://habr.com/ru/post/1570385/


All Articles