谁能告诉我为啥我收到错误 [AttributeError: 'list' object has no attribute 'encode']
Posted
技术标签:
【中文标题】谁能告诉我为啥我收到错误 [AttributeError: \'list\' object has no attribute \'encode\']【英文标题】:Can anyone tell my why I'm getting the error [AttributeError: 'list' object has no attribute 'encode']谁能告诉我为什么我收到错误 [AttributeError: 'list' object has no attribute 'encode'] 【发布时间】:2016-11-04 05:19:12 【问题描述】:我一直在尝试运行此代码,以便将 Excel 工作表作为电子邮件的附件发送。我可以使用 smtplib 发送普通电子邮件,但无法让 MIMEMultipart 工作。我不断收到 [AttributeError: 'list' object has no attribute 'encode'] 错误
import smtplib, ssl
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
fromaddr = ['Email']
sendto = ['Email']
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = sendto
msg['Subject'] = 'This is cool'
body = "this is the body of the text message"
msg.attach(MIMEText(body, 'plain'))
filename = 'Work.xlsx'
attachment = open('/home/mark/Work.xlsx', 'rb')
part = MIMEBase('application', "octet-stream")
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename= %s' % filename)
msg.attach(part)
smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login('email', 'password')
text = msg.as_string()
smtpObj.sendmail(fromaddr, sendto , text)
smtpObj.quit()
【问题讨论】:
我正在使用 Python 3.4.3,如果这有影响的话 包含整个回溯可能很有启发性,但我认为@Kevin 是对的。 【参考方案1】:fromaddr = ['Email']
sendto = ['Email']
这对我来说有点奇怪。它们不应该是字符串,而不是列表吗?
fromaddr = 'Email'
sendto = 'Email'
【讨论】:
【参考方案2】:我仍然遇到错误,所以我做了以下更改,它对我有用。
toaddr = ['mailid_1','mailid_2']
cc = ['mailid_3','mailid_4']
bcc = ['mailid_5','mailid_6']
subject = 'Email from Python Code'
fromaddr = 'sender_mailid'
message = "\n !! Hello... !!"
msg['From'] = fromaddr
msg['To'] = ', '.join(toaddr)
msg['Cc'] = ', '.join(cc)
msg['Bcc'] = ', '.join(bcc)
msg['Subject'] = subject
s.sendmail(fromaddr, (toaddr+cc+bcc) , message)
【讨论】:
以上是关于谁能告诉我为啥我收到错误 [AttributeError: 'list' object has no attribute 'encode']的主要内容,如果未能解决你的问题,请参考以下文章
谁能告诉我为啥我收到错误 [AttributeError: 'list' object has no attribute 'encode']
谁能告诉我为啥在尝试实现动画时在 Flutter 中出现此错误
开发服务器返回响应错误代码:500 react-native 为啥这个错误谁能告诉我