在Python中将变量传递给HTML电子邮件

时间:2015-08-20 22:40:09

标签: python email smtplib

我正在用Python发送电子邮件,并尝试在HTML中传递变量。我看到这可以通过"formatstring".format完成,但我无法成功完成。这是我所拥有的简短版本:

email_to = []
email_apiendpoint = []

for data_item in json.loads(email_curl_str, strict=False)['data']:
    email_to.append(data_item['emailAddress'])
    email_apiendpoint.append(data_item['apiEndpoint'])

ddn = random.randint(200, 400)

email_msg = MIMEMultipart('alternative')
email_msg['Subject'] = "Link"
email_msg['From'] = email_from
email_msg['To'] = email_to[0]

email_text = "Plain text here."
email_html = """\
<html>
<head>
</head>
<body>
<p>Compare to: {ddn}</p>
</body>
</html>
""".format(ddn=ddn)

part1 = MIMEText(email_text, 'plain')
part2 = MIMEText(email_html, 'html')

email_user = ''
email_pw = ''

smtpserver = smtplib.SMTP("mauimail.prxy.com")


def send_email():
    email_msg.attach(part1)
    email_msg.attach(part2)

    smtp.server.ehlo()
    smtpserver.starttls()
    smtpserver.ehlo
    smtpserver.login(email_user, email_pw)

    smtpserver.sendmail(email_from, email_to[0], email_msg.as_string())
    smtpserver.quit()

这导致:

""".format(ddn=ddn)
KeyError: ' border'

我做错了什么?

0 个答案:

没有答案