Jul-24-2020, 02:05 PM
I get the following errors from my code:
I am learning Python via project building and not tutorials because I can't learn general programming concepts from tutorials using bland short examples and apply it immediately to my project, unfortunately, so I need help here.
I would appreciate any pointers.
Error:attachment = open(Assessments, 'rb')
TypeError: expected str, bytes or os.PathLike object, not listError:]File "C:\Users\Matti\Desktop\[COPY]attaching MULTIPLE files.py", line 31, in <module>
file_path = os.path.join(dir_path, assess)
NameError: name 'dir_path' is not definedWhat I'm trying to do is send several attachments (files, not images) and not just one. I managed with one, but for some reason several is hard for me to grasp.I am learning Python via project building and not tutorials because I can't learn general programming concepts from tutorials using bland short examples and apply it immediately to my project, unfortunately, so I need help here.
I would appreciate any pointers.
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
#Notice we are using Windows "Enviornment Variables", in order to hide our sensitive data
Email_User = os.environ.get("Andree_Email")
Email_Password = os.environ.get("Andree_Pass")
Subject = "Python"
msg = MIMEMultipart()
msg['From'] = Email_User
msg['To'] = "[email protected]"
msg['Subject'] = Subject
body = "Hi there. Sending this email from Python"
msg.attach(MIMEText(body, 'plain'))
#EDITED
Assessments = ["c:\\Users\\Matti\Desktop\\Python video left off.docx", "c:\\Users\\Matti\Desktop\\API variable.png", "c:\\Users\\Matti\Desktop\\mind tricks.docx"]
attachment = open(Assessments, 'rb')
for assess in Assessments: # add files to the message
file_path = os.path.join(dir_path, assess)
attachment = MIMEApplication(open(file_path, "rb").read(), _subtype="txt")
attachment.add_header("c:\\Users\\Matti\Desktop\\Python video left off.docx","c:\\Users\\Matti\Desktop\\API variable.png","c:\\Users\\Matti\Desktop\\mind tricks.docx",'attachment', filename=assess)
msg.attach(attachment)
part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',"attachment; filename= "+Assessments)
msg.attach(part)
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(Email_User,Email_Password)
server.sendmail(Email_User,"[email protected]",text)
server.quit()
