0

试图通过 python 发送电子邮件。可以让它在正文中发送带有正确文本内容的电子邮件,但它正在打印实际的变量名称“新公司”而不是它的内容。这是代码和生成的电子邮件。

(不知道为什么 html 标签没有出现在下面的代码中,但我在电子邮件内容之前和之后使用了 html 和 body 标签)

任何和所有的帮助表示赞赏。

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

EMAIL_SERVER = 'blahblah'
EMAIL_PORT = 25

f = open('diff.txt', 'r')
NEW_COMPANIES  = f.read()


EMAIL_FROM = 'blabal'
RECIPIENT_LIST = ['blahblah']

msg = MIMEMultipart()
msg['From'] = EMAIL_FROM
msg['To'] = ", ".join(RECIPIENT_LIST)
msg['Subject'] = 'North Carolina New Business Registration'


body = '''<html>
<body>

New Business Names:<br><br>
body.format('NEW_COMPANIES')
<br><br>
Affiliate: r2thek
<br><br>
Website Link: 
https://www.sosnc.gov/online_services/search/by_title/_Business_Registration
</body>
</html>'''

body.format('NEW_COMPANIES')

msg.attach(MIMEText(body, 'html'))


smtpserver = smtplib.SMTP(EMAIL_SERVER, EMAIL_PORT)
smtpserver.sendmail(EMAIL_FROM, RECIPIENT_LIST, msg.as_string())
smtpserver.quit()

电子邮件结果:

新企业名称:

{NEW_COMPANIES}

附属机构:r2thek

网站链接:https ://www.sosnc.gov/online_services/search/by_title/_Business_Registration

4

1 回答 1

0

看起来您没有传递任何变量来格式化字符串,您需要这样做

body.format(variable1, variable2, etc)

在您的情况下,我认为它只是一个变量,因此无论您存储什么 NEW_COMPANIES 都需要用作 str.format() 的参数

于 2018-08-29T19:40:09.810 回答