str_htmlHeader = "<!DOCTYPE HTML PUBLIC>\n<html>\n";
str_htmlHeader += "<head>\n\t<title>Audi Cloud Services</title>\n</head>\n\n";
str_htmlHeader += "<body>\n\n<h1>Nightly build test results</h1>\n";
str_htmlFooter = "\n</body>\n\n</html>";
for root, dirnames, filenames in os.walk(r'\\ac-srvfile01\_Embedded\VCon1\proj_customer\337159_Audi_ACR_and_TSSS\pcm-audio'):
for filename in fnmatch.filter(filenames, '*.html'):
reportContent = open(os.path.join(root,filename)).read()
attachment = MIMEText(str_htmlHeader+reportContent+str_htmlFooter, 'html')
msg.attach(attachment)
#msg.attach(MIMEText(open(filename).read(), "text/html"))
我正在向相关人员发送消息,但该电子邮件将通过其他电子邮件发送给受尊敬的人。我想收集所有报告并将其作为单个电子邮件发送。但上面的代码将报告作为不同的电子邮件发送。有人可以帮我解决这个问题吗?
答案 0 :(得分:1)
您想要创建单个容器消息,然后在循环中为其添加MIME部分。
multipart/alternative
容器不适用于此,因为它表示客户端应选择其中一个部件进行显示,而忽略其余部分。我使用multipart/related
代替。您可能更喜欢multipart/mixed
,这并不意味着各部分之间存在某种关系。
str_htmlHeader = '''<!DOCTYPE HTML PUBLIC>
<html>
<head><title>Audvices</title></head>
<body><h1>Nightt results</h1>'''
str_htmlFooter = '''"
</body>\n\n</html>'''
msg = MIMEMultipart('related')
msg['From'] = 'ucd_test'
msg['To'] = 'hemappa@nce.com'
msg['Subject'] = 'AUDICES'
for root, dirnames, filenames in os.walk(r'\\ac-srvR_and_TSSS\pcm-audio'):
for filename in fnmatch.filter(filenames, '*.html'):
reportContent = open(os.path.join(root,filename)).read()
attachment = MIMEText(str_htmlHeader+reportContent+str_htmlFooter, 'html')
attachment.add_header("Content-Disposition", "attachment",\
filename=os.path.basename(filename))
msg.attach(attachment)
server = smtplib.SMTP('eu-smtp.nuance.com')
server.ehlo()
#server.starttls()
#server.login(username,password)
server.sendmail(fromaddress,toaddress.split(','),msg.as_string())
server.quit()