我有这个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()
我可以成功发送电子邮件给我想要的收件人但是当收件人收到电子邮件时,它只有一个空白的消息。任何评论都可以。提前谢谢!
答案 0 :(得分:1)
我要做的第一件事是使用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]) + "'")
在这种情况下,您可能(?)是安全的,因为您可以控制now
和row[0]
,但始终更好,更安全地使用预准备语句和将值作为参数传递,而不是将它们连接到查询中。
以下是如何在MySQLdb中使用预准备语句:
http://en.wikipedia.org/wiki/Prepared_statement#Python_DB-API
(忽略该示例是sqlite3,它的工作方式相同,因为它们都遵循python的DBAPI标准)
答案 1 :(得分:1)
邮件中的空格不正确。我相信你的消息是这样的:
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>"""
答案 2 :(得分:-1)
除非我没有误会,"""
相当于Python中的代码注释。
所以基本上你正在评论这条消息。
我用py编码已经很久了。不要伤害我......