*The code below shows part of my code and I want to use an open dialog module to select the file I would want to attach to my email. I am getting the following error:
Cannot attach additional subparts to non-multipart/
Please Assist me. I am using a button to open the dialog module thats why I created the function attach. I Have also attched a picture of my GUI application. Thank you
or","Please Enter The Correct Details!!!")
def attachment():
global file
file = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpg files","*.jpg"),("all files","*.*")))
print (file)
def mail():
try:
if send_email.get()=="" or send_pass.get()=="" or recv_email.get()=="":
messagebox.showerror("Error","Please Enter The Complete Details.")
else:
a=send_email.get()
b=send_pass.get()
c=msg_body.get('1.0',END)
d=recv_email.get()
subj = sub_email.get()
fromx = a
to = d
msg = MIMEText(c)
msg['Subject'] = subj
msg['From'] = fromx
msg['To'] = to
# Setup the attachment
filename = os.path.basename(file)
attachment = open(file, "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
# Attach the attachment to the MIMEMultipart object
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.ehlo()
server.login(a, b)
server.sendmail(fromx, to, msg.as_string())
server.quit()
msg_box()
except Exception as e:
print(e)
a=messagebox.askokcancel("Errror","Please Enter The Correct Details!!!")