为了账号安全,请及时绑定邮箱和手机立即绑定

在电子邮件正文中显示 Python HTML 表格

在电子邮件正文中显示 Python HTML 表格

HUWWW 2022-04-27 13:24:49
我编写了一个 python 脚本来查询我的数据库并以 HTML 表格格式显示数据。我怎样才能让这个代码以表格的形式显示到电子邮件中?我尝试将代码粘贴在第二个脚本(EMAIL)上的 html 标记中,但它不会仅读取 HTML 的 Python 代码。            import pyodbc            import cgi            def htmlTop():                print("""Content-type:text/html\n\n                      <!DOCTYPE html>                      <html>                      <head>                        <meta charset="utf-8"/>                        <title>My Tabmle</title>                        </head>                        <body>""")            def selectCOAStatus(cnxn, cursor):                cursor.execute('''SELECT * from mytable''')                data = cursor.fetchall()                return data            def htmlTail():                print("""</body>                    </html>""")            def connectDB():                conn_str = (                    r'DRIVER={SQL Server};'                    r'SERVER=test;'                    r'DATABASE=test;'                    r'Trusted_Connection=yes;'                )                cnxn = pyodbc.connect(conn_str)                cursor = cnxn.cursor()                return cnxn, cursor            def displayData(data):                print("<table border='1'")                print("<tr>")                print("<th>Date</th>")                print("<th>Count</th>")                print("<th>Status</th>")                print("</tr>")                for row in data:                    print("<tr>")                    print("<td>{0}</td>".format(row[0]))                    print("<td>{0}</td>".format(row[1]))                    print("<td>{0}</td>".format(row[2]))                    print("</tr>")                print("</table>")
查看完整描述

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)


查看完整回答
反对 回复 2022-04-27
?
精慕HU

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()


查看完整回答
反对 回复 2022-04-27
  • 2 回答
  • 0 关注
  • 593 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号