发送python html邮件不查看mai的消息

2024-05-21 06:24:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个python发送html邮件,在这个代码中,我可以发送电子邮件,但是消息的正文是空的。你知道吗

下面是我的代码

#!/usr/bin/python
import smtplib
import MySQLdb
import time
import os
from datetime import datetime

def timestamp():
        now = datetime.now()
        return now.strftime("%Y-%m-%d %H:%M:%S")
def write_log(log):
        date_today = datetime.now().strftime("%Y-%m-%d") 

    if os.path.isfile('logs/log.'+date_today+'.txt') == True:
            wlog = open('logs/log.'+date_today+'.txt','a')
            wlog.writelines('\n'+timestamp()+' - '+log)
    else:
            wlog = open('logs/log.'+date_today+'.txt','w')
            wlog.writelines(timestamp()+' - '+log)

def login():
    now = time.strftime("%c")
    username = "email@email.com"
    pwd = 'password'
    smtpserver = smtplib.SMTP("email@email.com",587)
    smtpserver.login(username, pwd)

db_ip = 'localhost'
db_user = 'USER'
db_pass = 'DBPASSWORD'
db_dbase = 'DBASE'

while 1:
    db = MySQLdb.connect(db_ip,db_user,db_pass,db_dbase)
    cur = db.cursor()
    cur.execute("SELECT * FROM form_number WHERE tag = 2")
    data = cur.fetchall()

    for row in data:
        recipient = row[6]
        sender = 'NO REPLY EMAIL<noreply@email.com>'
        assigned_person = row[5]
        header = 'Date: ' + now + '\n' + 'To:' + recipient + '\n' + 'From: ' +sender + '\n' + 'Subject:Liquidation record of ' + assigned_person +  '\n'
        msg = """Content-type: text/html
                    Subject: liquidation for """+ row[5] +"""
                            <font color='#000000'>
                                """ + row[5] +  """ has sent you a request.
                                to view click on the link below.
                             </font>"""
        message = header + msg
        print message
        smtpserver.sendmail(sender, recipient, message)
        cur.execute("UPDATE form_number SET tag = 3, time_sent = '" + now + "' WHERE form_number = '" + str(row[0]) + "'")

    db.commit()
    db.close()
    time.sleep(15)

def run():
    try:
        login()
    except:
        pass

run()
#login()

我可以成功地发送电子邮件给我想要的收件人,但当收件人收到电子邮件时,它只有一个空白的mesage。任何评论都可以。提前谢谢!你知道吗


Tags: importlogdbtodaydatetimedatetimeemail
3条回答

我要做的第一件事是使用python的email模块,而不是手工构建电子邮件。它将负责为HTML和纯文本正确构建信封。那里的文档甚至展示了如何使用smtplib发送HTML电子邮件(向下滚动到“如何创建HTML消息”部分)。你知道吗

另外,要小心这一行的SQL injection!你知道吗

cur.execute("UPDATE form_number SET tag = 3, time_sent = '" + now + "' WHERE form_number = '" + str(row[0]) + "'")

在这种情况下,您可以(?)请注意安全,因为您可以控制nowrow[0],但使用准备好的语句并将值作为参数传递,而不是将它们连接到查询中,这样做会更好、更安全。你知道吗

下面介绍如何在MySQLdb中使用准备好的语句:

http://en.wikipedia.org/wiki/Prepared_statement#Python_DB-API

(忽略示例是sqlite3,它的工作原理是相同的,因为它们都遵循python的DBAPI标准)

除非我没有搞错,"""相当于Python中的代码注释。你知道吗

所以基本上你是在评论信息。你知道吗

我很久没有用py编码了。别伤害我。。。

邮件中的空白不正确。我相信你的信息是这样的:

From: NO REPLY SMSGT<noreply@smsgt.com>
Subject:Liquidation record of assigned_person
Content-type: text/html
                Subject: liquidation for John
                        <font color='#000000'>
                            John has sent you a request.
                            to view click on the link below.
                         </font>

但应该是这样的:

From: NO REPLY SMSGT<noreply@smsgt.com>
Subject:Liquidation record of assigned_person
Content-type: text/html
Subject: liquidation for John

<font color='#000000'>
John has sent you a request.
to view click on the link below.
 </font>

尝试将赋值替换为msg,如下所示:

msg = \
"""Content-type: text/html
Subject: liquidation for """+ row[5] +"""

<font color='#000000'>
""" + row[5] +  """ has sent you a request.
to view click on the link below.
</font>"""

相关问题 更多 >