Python E-Mail sende mit Excel Anhang

Moin,
ich wollte mit Python regelmäßig E-Mails versenden an die jeweils eine kleine Excel Tabelle angehängt ist. Ich benutze dafür ein Gmail Konto. Es funktioniert auf jeden Fall solange ich keine Datei anhänge.

Diese Zeilen sind fehlerhaft:
SendMail.prepareMail(…)
und
part.set_payload(os.open(file), „rb“).read()) -> hier ist der Fehlercode "IsADirectoryError: [Errno 21] Is a directory: ‚/‘

Ich hoffe ihr könnt mir dabei helfen :smiley:

import sys, smtplib, os
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import formatdate
from email import encoders

class SendMail(object):
  mailadress = '[email protected]'
  smtpserver = 'smtp.googlemail.com'
  username = 'xxx'
  password = 'xxx'

def send(self, files):
  # Gather information, prepare mail
  to = self.mailadress
  From = self.mailadress
  #Subject contains preview of filenames
  if len(files) <= 3: subjAdd = ','.join(files)
  if len(files) > 3: subjAdd = ','.join(files[:3]) + '...'
  subject = 'Dateiupload: ' + subjAdd
  msg = self.prepareMail(From, to, subject, files)

#Connect to server and send mail
  server = smtplib.SMTP(self.smtpserver)
  server.ehlo() #Has something to do with sending information
  server.starttls() # Use encrypted SSL mode
  server.ehlo() # To make starttls work
  server.login(self.username, self.password)
  failed = server.sendmail(From, to, msg.as_string())
  server.quit()

def prepareMail(self, From, to, subject, attachments):
    msg = MIMEMultipart()
    msg['From'] = From
    msg['To'] = to
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject

# The Body message is empty
msg.attach( MIMEText("") )

for file in attachments:
  #We could check for mimetypes here, but I'm too lazy
  part = MIMEBase('application', "octet-stream")
  part.set_payload( open(os.open(file),"rb").read() )
  Encoders.encode_base64(part)
  part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file))
  msg.attach(part)
  #Delete created Tar
return msg

if __name__ == '__main__':
  mymail = SendMail()
  # Send all files included in command line arguments
  mymail.send(sys.argv[1:])
  
SendMail.prepareMail("[email protected]", "[email protected]", "[email protected]", "Titel 1", "/home/pi/Desktop/Teststand/export/protokoll/Protokoll04_May_2020.xlsx")