谁能告诉我为什么我收到错误信息[AttributeError:“列表"对象没有属性“编码"] [英] Can anyone tell my why I'm getting the error [AttributeError: 'list' object has no attribute 'encode']

查看:108
本文介绍了谁能告诉我为什么我收到错误信息[AttributeError:“列表"对象没有属性“编码"]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直尝试运行此代码,以便将Excel工作表作为附件发送到电子邮件中.我可以使用smtplib发送普通电子邮件,但无法使MIMEMultipart正常工作.我不断收到[AttributeError:'list'对象没有属性'encode']错误

I keep trying to run this code in order to send an excel sheet as an attachment on an email. I can send normal emails using smtplib but can't get the MIMEMultipart to work. I keep getting the [AttributeError: 'list' object has no attribute 'encode'] error

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()

推荐答案

fromaddr = ['Email']
sendto = ['Email']

这对我来说有点奇怪.它们不应该是字符串,不是列表吗?

This looks a little odd to me. Shouldn't they be strings, not lists?

fromaddr = 'Email'
sendto = 'Email'

这篇关于谁能告诉我为什么我收到错误信息[AttributeError:“列表"对象没有属性“编码"]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆