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

时间:2015-04-06 18:57:05

标签: python

我有三个.html文件的3个HTML表格,我需要使用Python一个在另一个下面发送电子邮件。

目前只有1张表被附加。如何附上所有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())

2 个答案:

答案 0 :(得分:2)

我有同样的要求。我使用了一个名为" report"的变量。其中包含所有html代码。所以我不断添加任意数量的表来报告可变性

report = ""
report += table1data
report += "<br><br>"
report += table2data
.
.

最后将此变量附加为电子邮件文本。

这对我有用。

答案 1 :(得分:0)

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

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