2016-12-04 64 views
0

所以我会尽力解释这一点,我可以做到最好。 我手边的任务是从数据库中收集数据,并将这些信息放入一个自定义的编码/格式化的HTML/CSS电子邮件中。通过电子邮件发送格式化的清单

运行此代码时出现的错误是:TypeError:无法隐式将'list'对象转换为'str'。 我从数据库中获得的数据在每个部分的列表中都带回了8个字符串的结果,所以我知道为什么这个错误会回来。我只是不太清楚如何绕过它,以便能够自动将所有数据嵌入到电子邮件中,并以适当格式发送此类电子邮件。

import smtplib 
import sys 
import sqlite3 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 

DATABASE = 'Blocs.db' 
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif']) 

print("Getting blocs from server...") 
conn = sqlite3.connect(DATABASE) 
cur = conn.cursor() 
cur.execute("SELECT weburl FROM Blocs") 
weburl_data = cur.fetchall() 
cur.execute("SELECT imgurl FROM Blocs") 
imgurl_data = cur.fetchall() 
cur.execute("SELECT title FROM Blocs") 
title_data = cur.fetchall() 
cur.execute("SELECT notes FROM Blocs") 
notes_data = cur.fetchall() 
conn.commit() 
conn.close() 

from_email = "[email protected]" 
from_pwd = "Password" 
to_email = "[email protected]" 

msg = MIMEMultipart('html') 
msg['Subject'] = "Test SMTPlib Message" 
msg['From'] = "[email protected]" 
msg['To'] = "[email protected]" 

firstHTML = '<html> <head></head> <body><table> <tr>' 
bloc_one = weburl_data 
secondHTML = '</tr></table></body></html>' 

new_html = firstHTML + bloc_one + secondHTML 

msg.attach(MIMEText(new_html, 'html')) 
print(msg) 

mail = smtplib.SMTP('outlook.office365.com', 587) 
mail.ehlo() 
mail.starttls() 
mail.login("[email protected]", "Password") 
mail.sendmail("[email protected]", "[email protected]", msg.as_string()) 
print("email sent") 
mail.quit() 
+1

或许你可以在哪一行说发生了错误?! – astraTiCon

+0

即时将假设错误发生在'new_html = firstHTML + bloc_one + secondHTML'。因为bloc_one仍然是一个列表。您应该将该列表转换为字符串,如下面的答案。 – putonspectacles

回答

0

我不是真的在邮件的发送专家部门,但如果你唯一的问题是转换列表为str为什么不尝试做与加入?

new_string = ''.join(name_of_the_list) 

我希望它能帮助:)

相关问题