I wrote a script that will download any attachments from unread messages in a mail box. The 'application/pdf' content type I'm not having any trouble with. The base64 Encoded 'application/octet-stream' content type is giving me a time.
My code below moves the pdf to the requested path, but corrupted as 0kb. Any assistance is appreciated.
try:
if part.get_content_type() == 'application/octet-stream':
payload = part.get_payload(decode=1)
fp = open(os.path.join('C:\\Attachment_Downloader\\',
datetime.datetime.now().strftime("%m%d%y%H%M%S")
+ "_" + str(var_seq) + ".pdf"), 'wb')
fp.write(base64.decodestring(payload))
fp.close()
logging.debug("File Decoded and Moved "+ part.get_filename())
except Exception as e:
logging.debug("File Move Failed : " + part.get_filename())
logging.exception("message")
I was decoding twice. I fixed my code to the below and it's working as expected:
try:
if part.get_content_type() == 'application/octet-stream':
payload = part.get_payload()
fp = open(os.path.join('C:\\Attachment_Downloader\\',
datetime.datetime.now().strftime("%m%d%y%H%M%S")
+ "_" + str(var_seq) + ".pdf"), 'wb')
fp.write(base64.decodestring(payload))
fp.close()
logging.debug("File Decoded and Moved "+ part.get_filename())
except Exception as e:
logging.debug("File Move Failed : " + part.get_filename())
logging.exception("message")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.