是否可以将多个html表放入使用smtplib
和email
发送的邮件中?每当我使用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
被附加。
答案 0 :(得分:0)
您可以尝试
part1.add_header('Content-Disposition', 'attachment', filename='part1.xml')
part2.add_header('Content-Disposition', 'attachment', filename='part2.xml')
或
msg1 = MIMEMultipart('alternative')
答案 1 :(得分: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()