1

Can you use JavaScript inside an email, which is sent using Python?

My aim is to send a working clock inside an email.

I am trying to use Python to do so. I am using IDLE and the Python libraries stmplib, email and html2text to send emails. My code looks like the code shown below. I omitted some details (style and script) as they are unimportant.

me = "my.email@gmail.com"
you = "my.email@gmail.com"

msg = MIMEMultipart('alternative')
msg['Subject'] = "Clock"
msg['From'] = me
msg['To'] = you

html = """\
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Clock</title>
        <style></style>
    </head>
    <body>
        <div class="clock">
            <p class="clockhour">HH</p>
            <p class="clocksym1">:</p>
            <p class="clockminute">MM</p>
            <p class="clocksym2">:</p>
            <p class="clocksecond">SS</p>
        </div>
        <script type="text/javascript"></script>
    </body>
</html>
"""

text = html2text(html)

part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

msg.attach(part1)
msg.attach(part2)

s = SMTP('smtp.gmail.com:587')
s.ehlo()
s.starttls()
s.login("my.email@gmail.com", "my password")

s.sendmail(me, you, msg.as_string())
s.quit()

The full version of the HTML in this code gives me a properly functioning clock, but if I attempt sending it to myself using Gmail I receive a different result.

Viewing the HTML:

enter image description here

In Gmail:

enter image description here

This shows, that parts of the CSS load, but others like the font or the font size don't. It also shows that the JavaScript does not load. (Normally viewing the HTML code in a browser gives a functioning clock, while in an email doesn't.)

Is there a way to send an email with this clock?

Ron Lauterbach
  • 107
  • 1
  • 1
  • 12

2 Answers2

2

Short Answer: No. You can't use JavaScript for email template.


Tricky way: You can work on relevant file and get the parsed value in your template file using server side language.

Example:

clock.js
// code for js

template.php
// echo the value
// The value is rendered in html and works in email template too.

But this case is not suitable for you as you're trying to implement countdown clock. This is suitable only for static value.

However, linking to external page content will help you to show the timer.


For your case, You may try using http://motionmailapp.com/

Hope, this helps!

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
1

javascript is unsupported in email html, but at least you can place a link to the page with clock countdown.

Sergey Sklyar
  • 1,902
  • 1
  • 15
  • 27