gpt4 book ai didi

php - 发送python html邮件看不到邮件的内容

转载 作者:太空宇宙 更新时间:2023-11-03 18:12:37 24 4
gpt4 key购买 nike

我有这个 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()

我可以成功发送电子邮件给我想要的收件人,但当收件人收到电子邮件时,只有一条空白消息。任何评论都可以。提前致谢!

最佳答案

您的消息中的空格不正确。我相信您的消息如下所示:

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>"""

关于php - 发送python html邮件看不到邮件的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25615133/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com