serickson
serickson

Reputation: 11

Simple HTML Email: Basic CSS styles being stripped

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.

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.

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(
        "[email protected]",
        "[email protected]",
        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)

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?

Upvotes: 0

Views: 596

Answers (1)

user4521733
user4521733

Reputation:

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.

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

Upvotes: 1

Related Questions