在电子邮件正文中打印列表元素 - 'list'对象没有属性'encode'

时间:2017-06-17 14:36:16

标签: python python-2.7 email

我正在制作一个脚本,用于检查网站上对我感兴趣的拍卖。如果找到感兴趣的链接,则会将此链接添加到listalink listalink.append(link)。当我发送电子邮件时,我有这个错误:

  

AttributeError:'list'对象没有属性'encode'。

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

# listalink example:
listalink = ["http://www.google.pl", "http://www.facebook.com", "http://amazon.com"]

def email_sender():
   fromaddr = "test_e_mail@wp.pl"
   toaddr = "myemail@gmail.com"
   msg = MIMEMultipart()
   msg['From'] = fromaddr
   msg['To'] = toaddr
   msg['Subject'] = "NEW INTERESTED AUCTIONS"
   body = listalink
   msg.attach(MIMEText(body, 'plain'))
   server_ssl = smtplib.SMTP_SSL("smtp.wp.pl", 465)
   server_ssl.ehlo()
   server_ssl.login("test_e_mail@wp.pl", "password")
   text = msg.as_string()
   server_ssl.sendmail(fromaddr, toaddr, text)
   server_ssl.close()
   print 'E-mail sent'

1 个答案:

答案 0 :(得分:2)

发生错误是因为您将list添加到电子邮件正文(正文必须为str):

body = listalink

解决方案:

listalink = ["http://www.google.pl", "http://www.facebook.com", "http://amazon.com"]
listalink = " ".join(listalink)