import 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
os.chdir(path)
def send_mail(send_from,send_to,subject,text,files,server,port,username='',password='',isTls=True):
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = send_to
msg['Date'] = formatdate(localtime = True)
msg['Subject'] = 'Test'
msg.attach(MIMEText(text))
part = MIMEBase('application', "octet-stream")
part.set_payload(open("Olaf.xlsx", "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment;
filename="Olaf.xlsx"')
msg.attach(part)
smtp = smtplib.SMTP('smtp.web.de', 587)
smtp.starttls()
smtp.login('test@test.de', 'pw')
At this part the error occurs: NameError: name 'msg' is not defined. But whats wrong? Here is where I got the code from: add excel file attachment when sending python email
smtp.sendmail('xy','xyz', msg.as_string())
smtp.quit()