0

Python で MySql クエリを実行しているコードがあります。次に、メールで送信される HTML ファイルとしてクエリを返します。

すべてが実行されますが、送信された電子メールは<TABLE>本文に表示されます。

このチュートリアルを使用しましたが、何が問題なのかわかりません。助けてください。

import pymysql
import os
import HTML

conn = pymysql.connect(host='localhost', port=1000, user='users', passwd='pw', db='database')
cur = conn.cursor()

#mysql query
query = ("""select * from table""")
cur.execute(query)

rows = cur.fetchall()
for row in rows:
    for col in row:
        print "%s," % col
    print "\n"

htmlcode = HTML.table(rows)
print htmlcode

import smtplib
content = 'email report'

mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo()
mail.starttls()
mail.login('email@email.com', 'pw')
mail.sendmail('email@email.com', 'email@email.com', htmlcode)
mail.close()

電子メール メッセージに移動すると、元のテキストと HTML クエリが表示されますが、電子メールの本文には表示されません。

私が見逃している議論は何ですか?

4

1 に答える 1

0

私はこの問題の答えを見つけました。この例Sending HTML email using Python を使用し、問題に実装しました

import pymysql
import os
import HTML
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


conn = pymysql.connect(host='localhost', port=1000, user='users', passwd='pw', db='database')

cur = conn.cursor()

#mysql query

query = ("""select * from table""")

cur.execute(query)

htmlcode = HTML.table(rows)

# me == my email address
# you == recipient's email address
me = "me@me.com"
you = "you@you.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 = htmlcode

# 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.
mail = smtplib.SMTP('smtp.gmail.com', 587)

mail.ehlo()

mail.starttls()

mail.login('userName', 'password')
mail.sendmail(me, you, msg.as_string())
mail.quit()
于 2014-10-14T19:51:49.043 に答える