I have mail messages which contain attachments.
I need to upload the attachments from mail to google drive.
For mail I'm using imaplib and for google drive I'm using pyDrive
I'm using the below code to get attachment:
if mail.get_content_maintype() == 'multipart':
for part in mail.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
attachment = part.get_payload(decode=True)
I have payload
my attachment in mail. Now I can't understand how upload payload
to google drive using pyDrive. I've tried this, but it did not work
attachment = part.get_payload(decode=True)
gd_file = self.gd_box.g_drive.CreateFile({'title': 'Hello.jpg',
"parents": [{"kind": "drive#fileLink", "id": folder['id']}]})
gd_file.GetContentFile(attachment)
gd_file.Upload()
UPD:
This code is work, but i think its bad solution(we save image localy, then upload this image in google drive)
attachment = part.get_payload(decode=True)
att_path = os.path.join("", part.get_filename())
if not os.path.isfile(att_path):
fp = open(att_path, 'wb')
fp.write(attachment)
fp.close()
gd_file = self.gd_box.g_drive.CreateFile({'title': part.get_filename(),
"parents": [{"kind": "drive#fileLink", "id": folder['id']}]})
gd_file.SetContentFile(part.get_filename())
gd_file.Upload()