I’m encountering a sending error when trying to attach one or more files to an email using Python 3.
The script works with embedded images, styled text,…etc. It’s the block of code below that makes it fail if a document is listed. In this example, the file does exist (in this case on the Mac Desktop) and the path is correct. The script runs to conclusion and appears to send the email but the email fails to go through.
The script ends with except: print(msg.as_string())
, which shows Sending Error.
#Attach Any Files
if '''/Users/Me/Desktop/Document.pdf''' != "":
files = '''/Users/Me/Desktop/Document.pdf'''
for file in files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(file,"rb").read() )
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="{0}"'.format(os.path.basename(file)))
msg.attach(part)