Python 在电子邮件中发送多个HTML表

Python 在电子邮件中发送多个HTML表,python,Python,我有三个.HTML文件中的3个HTML表,需要使用Python一个接一个地发送电子邮件 目前只有一个表被附加。如何连接所有3个 #!/usr/bin/python import time import os,sys from os import path import re import sys, ast import subprocess # Import smtplib for the actual sending function from email.mime.text import

我有三个.HTML文件中的3个HTML表,需要使用Python一个接一个地发送电子邮件

目前只有一个表被附加。如何连接所有3个

#!/usr/bin/python
import time
import os,sys
from os import path
import re
import sys, ast
import subprocess
# Import smtplib for the actual sending function

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from subprocess import Popen, PIPE
msg = MIMEMultipart('alternative')
# Open a plain text file for reading.  For this example, assume that
# the text file contains only ASCII characters.

html = open('/root/madhu_test/bpstest/results/outnss.html')
htmla = open('/root/madhu_test/bpstest/results/outs2c.html')
htmlb = open('/root/madhu_test/bpstest/results/outrecommended.html')
html = html.read()
htmla = htmla.read()
htmlb = htmlb.read()

part2 = MIMEText(html, 'html')
part3 = MIMEText(htmla, 'html')
part4 = MIMEText(htmlb, 'html')


msg.attach(part2)
msg.attach(part3) 
msg.attach(part4)


msg["From"] = "sauravb@juniper.net"
msg["To"] = "sauravb@juniper.net"
msg["Subject"] = "Sanity performance report"
p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE)
p.communicate(msg.as_string())

我也有同样的要求。我使用了一个名为“report”的变量,其中包含所有html代码。所以我一直添加任意数量的表来报告变量

report = ""
report += table1data
report += "<br><br>"
report += table2data
.
.
report=“”
报告+=表1数据
报告+=“

” 报告+=表2数据 . .
最后将此变量作为电子邮件文本附加


这对我很有效。

一个更好的解决方案可能是将
MIMEMultipart('alternative')
更改为
MIMEMultipart('mixed')


通过这种方式,它会附加每个附件,而不是为电子邮件客户端选择最匹配的附件。

是否要合并所有html文件并作为一封电子邮件发送?当前代码的结果如何?是的。目前只添加了第一个附件。如果它解决了您的问题,请将其标记为答案。非常感谢。