我试图在电子邮件中发送html表格。我不确定这是不是最好的方法,但现在我读了CSV,创建了一个表格,然后通过电子邮件发送信息。
这是我到目前为止所做的:
def create_table():
print("<html><table>")
# column headers
print("<th>")
print("<td>Tech</td>")
print("<td>Total</td>")
print("<td>Average</td>")
print("</th>")
infile = open("test.csv","r")
for line in infile:
row = line.split(",")
tech = row[0]
total = row[1]
average = row[2]
print("<td>%s</td>" % tech)
print("<td>%s</td>" % total)
print("<td>%s</td>" % average)
print("</table></html>")
SERVER = "localhost"
FROM = "forsdani@amazon.com"
TO = ["forsdani@amazon.com"] # must be a list
SUBJECT = "this is my email"
TEXT = "TABLE GOES HERE?"
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
# Send the mail
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()
如果我做的话
TEXT = create_table()
然后我得到&#34;无&#34;对于电子邮件中的文本。我不确定如何纠正从函数到电子邮件正文的数据。
答案 0 :(得分:1)
您的函数不会返回任何值,只会打印它们。 而不是像:
def create_table():
print("Hello, World!")
output = create_table() # output is None
尝试:
def create_table():
string_to_return = "Hello, World!"
return string_to_return
output = create_table() # output is "Hello, World!"
答案 1 :(得分:0)
这里的函数已更改为返回值:
def create_table():
message = ""
message += "<html><table>" + "\n"
# column headers
message += "<th>" + "\n"
message += "<td>Tech</td>" + "\n"
message += "<td>Total</td>" + "\n"
message += "<td>Average</td>" + "\n"
message += "</th>" + "\n"
infile = open("test.csv","r")
for line in infile:
row = line.split(",")
tech = row[0]
total = row[1]
average = row[2]
message += ("<td>%s</td>" % tech) + "\n"
message += ("<td>%s</td>" % total) + "\n"
message += ("<td>%s</td>" % average) + "\n"
message += ("</table></html>") + "\n"
return message