leforestier / yattag

Python library to generate HTML or XML in a readable, concise and pythonic way.
328 stars 31 forks source link

Tags Errors on Sended E-Mails #65

Closed kemalkolcuoglu closed 4 years ago

kemalkolcuoglu commented 4 years ago

Hello All,

I use yattag to generate reports of our services and I send the HTML output via e-mail to people. I have an issue with generating HTML output. In the code, as you can see, every tag proper with syntax and structure. But on EMails, we have problems.

E-Mail Outputs:

Tags have extra space or generate extra tag

Output of Issue: <td>N/A&lt; /td&gt;</td>

Picture of Issue:

Tags generated anomaly:

Output of Issue:

<tr>
<td>StartDate</td>
<td>0</td>
<t d="">July 18, 2018
<td>779 Days Past</td>
</tr>

Picture of Issue:

Code:

def generate_html_template(self, datas):
    counter = 0
    doc, tag, text, line = Doc().ttl()
    txt_style = 'body{padding:20px 30px;font-family:"Open Sans";} \
    table{background-color:#eaeaea;width:80%;border-collapse:collapse;border-spacing:0;margin:20px 20} \
    table,th{border:1px solid #fff;padding:8px 10px;text-align:center;font-size:1.3em;font-weight:700} \
    table,td{border:1px solid #fff;padding:10px 10px;text-align:center;font-size:1.0em} \
    .header{color:#fff!important;background-color:#F24080;font-size:1.1em}'
    doc.asis('<!DOCTYPE html>')
    with tag('html', lang='en', xmlns='http://www.w3.org/1999/xhtml'):
        with tag('head'):
            doc.stag('meta', charset='utf-8')
            with tag('style'):
                text(txt_style)
        with tag('body'):
            for key in datas:
                with tag('table'):
                    with tag('tr'):
                        with tag('th', colspan="4"):
                            text("HEADER")
                    with tag('tr'):
                        for item in ("Column1", "Column2", "Column3", "Column4"):
                            with tag('td', klass="header"):
                                text(item)
                    for item in datas[key]:
                        with tag('tr'):
                            with tag('td'):
                                text(item['Name'])
                            with tag('td'):
                                text(item['CurrentValue'])
                            with tag('td'):
                                if(not(is_digit(item['MaxValue'])) and is_date(item['MaxValue'])):
                                    item['MaxValue'] = datetime.strptime(item['MaxValue'], "%Y-%m-%dT%H:%M:%S.%f").strftime("%B %d, %Y")
                                text(item['MaxValue'])
                            with(tag('td')):
                                if (item['Name'] == 'Expiry Date') and is_date(item['MaxValue']):
                                    days = calculate_expiry_days(item['MaxValue'])
                                    if days <= 30:
                                        self.status_list.append("critical")
                                        with tag('p', style='color:#FF0000'):
                                            text('Critical {} Days Left'.format(days))
                                    elif days > 30 and days <= 40:
                                        self.status_list.append("warning")
                                        with tag('p', style='color:#FF9900'):
                                            text('Warning {} Days Left'.format(days))
                                    else:
                                        with tag('p', style='color:#05F715'):
                                            text('{} Days Left'.format(days))
                                elif (item['Name'] == 'Start Date' or item['Name'] == 'SeatStartDate') and is_date(item['MaxValue']):
                                    days = calculate_past_days(item['MaxValue'])
                                    text("{} Days Past".format(days))
                                else:
                                    if is_digit(item['MaxValue']) and (int(item['MaxValue']) != 0) and is_digit(item['CurrentValue']):
                                        try:
                                            threshold =  (float(item['CurrentValue']) * 100) / float(item['MaxValue'])
                                        except ZeroDivisionError:
                                            threshold = 0
                                        if threshold >= 80.0:
                                            self.status_list.append("critical")
                                            with tag('p', style='color:#FF0000'):
                                                text('Critical %{:.2f}'.format(threshold))
                                        elif threshold >= 70.0 and threshold < 80.0:
                                            self.status_list.append("warning")
                                            with tag('p', style='color:#FF9900'):
                                                text('Warning %{:.2f}'.format(threshold))
                                        else:
                                            with tag('p', style='color:#05F715'):
                                                text('No-Alarm %{:.2f}'.format(threshold))
                                    else:
                                        text('N/A')
                with tag('br'):
                    text(' ')
                counter += 1
    return doc.getvalue(), self.status_list

So, I really don't understand why I get these issues. And the main problem is this not be every time. Different days, different machines make these issues.

May somebody has any idea about this issue?

Thanks & Regards, KK

kemalkolcuoglu commented 4 years ago

Hello All,

The issue been on Mail server. I set utf-8 format to encode and it works fine.

FYI. KK