2 回答

TA贡献1798条经验 获得超7个赞
尝试
print("<td>%s</td>" % row[0] )
也是一种更简单的 html 电子邮件方法
from mailer import Mailer
from mailer import Message
message = Message(From="me@example.com",
To="you@example.com")
message.Subject = "An HTML Email"
message.Html = """<p>Hi!<br>
How are you?<br></p>"""
sender = Mailer('smtp.example.com')
sender.send(message)

TA贡献1845条经验 获得超8个赞
我能够将数据放入表中。我修复了我的 for 循环以遍历所有行并返回值。它只返回一行,而我的查询返回多行。为什么会这样?
导入pyodbc 导入cgi 导入html
conn_str = (
r'DRIVER=test'
r'SERVER=test;'
r'DATABASE=test;'
r'Trusted_Connection=yes;'
)
cnxn = pyodbc.connect(conn_str)
cursor = cnxn.cursor()
cursor.execute('''SELECT * from my table '''
for row in cursor:
html_code = """
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<body>
<table border='1'
<tr>
<th>Date</th>
<th>Count</th>
<th>Status</th>
</tr>
<tr>
<td>{}</td>
<td>{}</td>
<td>{}</td>
</tr>
</table>
</body>
</html>""".format(row[0],row[1],row[2])
print(html_code)
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# me == my email address
# you == recipient's email address
me = "test@aol.com"
you = "test@aol.com"
# Create message container - the correct MIME type is
multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you
# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nHow are you?\nHere is the link you
wanted:\nhttp://www.python.org"
html = html_code
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this
case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
# Send the message via local SMTP server.
s = smtplib.SMTP('email.fpl.com')
# sendmail function takes 3 arguments: sender's address, recipient's
address
# and message to send - here it is sent as one string.
s.sendmail(me, you, msg.as_string())
s.quit()
添加回答
举报