从Python在邮件中打印多个HTML表格
可以在用 smtplib
和 email
发送的消息中放入多个 HTML 表格吗?每次我用 attach()
添加多个内容时,它只会添加第一个。
具体来说,像这样做:
msg1 = MIMEMultipart('alternative')
msg1['Subject]' = ' '
msg1['From'] = me
msg1['To'] = you
part1 = MIMEText(fhtml, 'html')
part2 = MIMEText(dhtml, 'html')
msg1.attach(part1)
msg1.attach(part2)
s = smtplib.SMTP()
s.connect('mailserver')
s.sendmail(me, you, msg1.as_string())
s.quit
是行不通的。只有 part1
被添加上了。
2 个回答
0
我也遇到过同样的问题。这里有个我做的示例。这个想法是把所有的csv文件转换成html表格,然后把它们作为一个html发送。在这个例子中,有3个csv文件。我想把csv0.csv作为附件,csv1.csv和csv2.csv则作为html表格放在邮件正文里。
#!/usr/bin/env python
import csv, os
from tabulate import tabulate
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.MIMEText import MIMEText
import smtplib
import mimetypes
from email import encoders
from email.message import Message
def csv2htmlTableStr(CsvFileName):
# this function is adapted from
# https://stackoverflow.com/questions/36856011/convert-csv-to-a-html-table-format-and-store-in-a-html-file
#
# Open the CSV file for reading
reader = csv.reader(open(CsvFileName))
# initialize rownum variable
rownum = 0
# write <table> tag
HtmlStr = ''
HtmlStr += '<table>'
# generate table contents
for row in reader: # Read a single row from the CSV file
# write header row. assumes first row in csv contains header
if rownum == 0:
HtmlStr += '<tr>' # write <tr> tag
for column in row:
HtmlStr += '<th>' + column + '</th>'
HtmlStr += '</tr>'
#write all other rows
else:
HtmlStr += '<tr>'
for column in row:
HtmlStr += '<td>' + column + '</td>'
HtmlStr += '</tr>'
#increment row count
rownum += 1
# write </table> tag
HtmlStr += '</table>'
# print results to shell
#print "Created " + str(rownum) + " row table."
return HtmlStr
me = "<from email>" # REPLACE THIS
you = '<to email>' # REPLACE THIS
csv0 = "/tmp/csv_as_attachment.csv"
csv1 = "/tmp/csv_as_html_table1.csv"
csv2 = "/tmp/csv_as_html_table2.csv"
ctype, encoding = mimetypes.guess_type(csv0)
if ctype is None or encoding is not None:
ctype = "application/octet-stream"
maintype, subtype = ctype.split("/", 1)
fp = open(csv0)
# Note: we should handle calculating the charset
attachment = MIMEText(fp.read(), _subtype=subtype)
fp.close()
csv1Str = csv2htmlTableStr(csv1)
csv2Str = csv2htmlTableStr(csv2)
text = """
Here is the first table:
""" + csv1Str + """
Here is another table:
""" + csv2Str + """
"""
html = """
<html><body>
<p><b><font color='red'>Here is the first table:</font></b></p>
""" + csv1Str + """</p>
<p><b><font color='red'>Here is another table:</font></b></p>
""" + csv2Str + """</p>
</body></html>
"""
message = MIMEMultipart(
"alternative", None, [MIMEText(text), MIMEText(html,'html')])
message['Subject'] = "Try to embed more than one html tables in email body"
message['From'] = me
message['To'] = you
attachment.add_header("Content-Disposition", "attachment", filename='csv0')
message.attach(attachment)
server = smtplib.SMTP('localhost')
server.sendmail(me, [you], message.as_string())
server.quit()
0
你可以试试这个
part1.add_header('Content-Disposition', 'attachment', filename='part1.xml')
part2.add_header('Content-Disposition', 'attachment', filename='part2.xml')
或者这个
msg1 = MIMEMultipart('alternative')