pythonexcelsmtpattachment

Can anyone tell my why I'm getting the error [AttributeError: 'list' object has no attribute '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()

Solution

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

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

    fromaddr = 'Email'
    sendto = 'Email'