Search code examples
pythonhtmlhtml-tablehtml-emailtablesorter

Tablesorter not working when sent as HTML Email


I'm trying to email a table that will sort based on column when that column is clicked.

I found http://tablesorter.com/docs/ and it seemed pretty straight forward.

I'm using python to write the html. The function in question is

def getOpportunitiesTable(opps): #formats html table for email

It takes in an list of dict's and should return the html

html = ""
html += "<head>"
html += '<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>'
html += '<script type="text/javascript" src="http://mottie.github.com/tablesorter/js/jquery.tablesorter.js"></script>'
html += "</head>"
html += '<table id="opportunities" class="tablesorter">'
html += '<thead><tr><th>Opportunity Name</th><th>Forcasted Close Date</th><th>Pipeline Category</th><th>Stage</th><th>Priority</th></tr></thead>'

html += '<tbody>'
for o in iter(opps):
    html += "<tr>"
    #opportunity name linked to insightly's page

    html += "<td>" + '<a href="https://googleapps.insight.ly/Opportunities/Details/' + str(o['OPPORTUNITY_ID']) + '">'
    try:
        html += o['OPPORTUNITY_NAME'].encode('utf-8')
    except:
        html += '************************'
        logging.info(str(o))
    html += "</a>" + "</td>"
    html += "<td>"
    if 'FORECAST_CLOSE_DATE' in o and o["FORECAST_CLOSE_DATE"] != None: #if fclose date provided format it
        html += datetime.strptime(o["FORECAST_CLOSE_DATE"], '%Y-%m-%d %H:%M:%S').strftime('%Y-%m-%d')
    else: 
        html += ' - '
    html += "</td>"

    html += "<td>" + pipelineCategory[o['PIPELINE_ID']] if o['PIPELINE_ID'] in pipelineCategory else ' - ' + "</td>"
    html += "<td>" + stageName[o['STAGE_ID']] if o['STAGE_ID'] in stageName else ' - ' + "</td>"
    html += "<td>" + str(o["OPPORTUNITY_FIELD_5"]) + "</td>"
    html += "</tr>"

html += '</tbody>'   
html += "</table><br/>"

return html  

My confusion comes with these two lines

html += '<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>'
html += '<script type="text/javascript" src="http://mottie.github.com/tablesorter/js/jquery.tablesorter.js"></script>'

Also the inherent question of should I be allowed to send an html email that manipulates the table that is sent?

Any help is appreciated!


Solution

  • It isn't possible to use Javascript in HTML emails as this poses a large security risk. Most mail clients will strip out or simply ignore any scripts in the HTML message.

    In addition, loading of the remote scripts would likely be blocked by many clients as remote content and would require the user to manually load the remote content. But even if they were to allow that, it still doesn't allow one to run scripts in email.

    Your best bet would be to include that HTML as an attachment which they could then open (at their own risk), or save the HTML on your server somewhere and link to it.