繁体   English   中英

将变量的内容打印到电子邮件正文 python 3.7

[英]Print Contents of Variables to body of email python 3.7

尝试通过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

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

body.format(variable1, variable2, etc)

在你的情况下,我认为它只是一个变量,所以无论你在哪里存储 NEW_COMPANIES 应该被用作 str.format() 的参数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM