1

I am trying to write a python program that sends an email to me depending on some condition. I would like the value to be printed in red if it matches certain criteria and green otherwise. Below is a small snippet of my code. I'm unsure on how to add this jquery/Javascript part to it.

import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

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

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

text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
#a=0
#if a==0:
#       color="blue"
#else:
#       color="red"
html = """
<html>
  <head>
      <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
    <script>
                var a=0;
                 if (a<2) {
                  $("p").css("color","red");
                  }
                </script>
  </head>
  <body>
        <p>Hi!<br>
               How are you?<br>
                      Here is the <a
                      href='http://www.python.org'>link</a> you
                      wanted.
                          </p>
                            </body>
                            </html>
                            """


part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
s = smtplib.SMTP('localhost')
s.sendmail(me, you, msg.as_string())
s.quit()
J. Doe
  • 29
  • 1
  • 6
  • What is the problem with this code? –  Nov 28 '17 at 23:03
  • I would be surprised if mail clients execute JavaScript. Maybe you could add a ` –  Nov 28 '17 at 23:08

1 Answers1

1

Javascript in emails is not (and should not) be executed, at least in received emails for obvious security reasons. Shortly: Don't use javascript for this.

Since you're using Python, I would recommend using a templating engine such as Jinja2 to create the HTML. The condition can then be checked on the sender-side before the message is even send.

For example:

from jinja2 import Environment, PackageLoader, select_autoescape

env = Environment(
    loader=PackageLoader('yourapp', 'templates'),
    autoescape=select_autoescape(['html'])
)

template = env.get_template('email.html')
data = {'message': 'Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org'}

email = template.render(data)

# Rest of code to send the email here.

This assumes that there is a templates/ folder where your templates are stored. In this case there should be a html file templates/email.html

The email.html can now be fairly simple:

<html>
<head>
<style>
  <!-- This only adds the css if the expression is true -->
  {% if some_variable_from_context > 2 %}
  p { color: red; }
  {% endif %}
</style>
</head>
<body>
  <!-- This loads the message (see the template.render(data)) -->
  {{ message }}
</body>
</html>

Simpler solution:

Just use CSS. Make a CSS class that adds the style you want if the condition is true, and only add the class to the object you want if the condition is true. Now you can simply do the condition checking in your Python code, without having to rely on jquery / javascript, which will likely not work in emails anyway.

randyr
  • 1,679
  • 1
  • 11
  • 17