Can anyone tell my why I'm getting the error [AttributeError: 'list' object has no attribute 'encode']
Can anyone tell my why I'm getting the error [AttributeError: 'list' object has no attribute 'encode']
我一直在尝试 运行 此代码,以便将 excel sheet 作为电子邮件附件发送。我可以使用 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()
fromaddr = ['Email']
sendto = ['Email']
我觉得这有点奇怪。它们不应该是字符串,而不是列表吗?
fromaddr = 'Email'
sendto = 'Email'
我仍然遇到错误,所以我做了以下更改并且对我有用。
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)
我一直在尝试 运行 此代码,以便将 excel sheet 作为电子邮件附件发送。我可以使用 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()
fromaddr = ['Email']
sendto = ['Email']
我觉得这有点奇怪。它们不应该是字符串,而不是列表吗?
fromaddr = 'Email'
sendto = 'Email'
我仍然遇到错误,所以我做了以下更改并且对我有用。
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)