【发布时间】:2018-05-12 12:58:28
【问题描述】:
我正在尝试编写一个 python 程序,根据某些情况向我发送电子邮件。如果值符合某些条件,我希望以红色打印,否则以绿色打印。下面是我的代码的一个小sn-p。我不确定如何添加这个 jquery/Javascript 部分。
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()
【问题讨论】:
-
这段代码有什么问题?
-
如果邮件客户端执行 JavaScript,我会感到惊讶。也许您可以在 HTML 中添加
<style>部分,或者只使用内联样式?
标签: jquery python html css email