我正在尝试使用python将txt文件附加到电子邮件中,我似乎无法弄清为什么它无法编译
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import smtplib
email_user = 'xxx@gmail.com'
email_send = 'xxx@gmail.com'
subject= 'Test'
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
#msg['Cc'] = ''
#msg['Bcc'] = ''
msg['Subject'] = subject
body = 'Hello world'
msg.attach(MIMEText(body, 'plain'))
filename = 'Document.txt'
attachment = open(filename,'rb')
part= MIMEBase('application', 'octet-stream')
part.set_payload(attachment).read()
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment', filename= +filename)
msg.attach(part)
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email_user,'xxx')
server.sendmail(email_user, email_send, text)
server.quit()
我得到的错误是
”第27行,在 part.set_payload(attachment).read()
AttributeError:“ NoneType”对象没有属性“ read””